question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot import axios into razzle project using Typescript

See original GitHub issue

❓Question

I’m using Typescript and I am wanting to import axios.

Whenever I add an import, the build process complains about a TypeError inside the process/browser.js module.

I am struggling to make sense of this. If anyone is able to assist on where to start, should I look into the Babel project itself for the type definition?

Or abandon using Axios all together (highly undesired)

Thank you in advance.

[
  './node_modules/process/browser.js\n' +
    'Module build failed (from ./node_modules/razzle/config/babel-loader/razzle-babel-loader.js):\n' +
    'TypeError: /Volumes/code/name-of-project/node_modules/process/browser.js: Property left of AssignmentExpression expected node to be of a type ["LVal"] but instead got "BooleanLiteral"\n' +
    '    at Object.validate (/Volumes/code/name-of-project/node_modules/@babel/types/lib/definitions/utils.js:130:11)\n' +
    '    at validateField (/Volumes/code/name-of-project/node_modules/@babel/types/lib/validators/validate.js:24:9)\n' +
    '    at validate (/Volumes/code/name-of-project/node_modules/@babel/types/lib/validators/validate.js:17:3)\n' +
    '    at NodePath._replaceWith (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/path/replacement.js:170:5)\n' +
    '    at NodePath.replaceWith (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/path/replacement.js:152:8)\n' +
    '    at replaceAndEvaluateNode (/Volumes/code/name-of-project/node_modules/babel-plugin-transform-define/lib/index.js:37:12)\n' +
    '    at processNode (/Volumes/code/name-of-project/node_modules/babel-plugin-transform-define/lib/index.js:62:5)\n' +
    '    at PluginPass.MemberExpression (/Volumes/code/name-of-project/node_modules/babel-plugin-transform-define/lib/index.js:99:9)\n' +
    '    at newFn (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/visitors.js:177:21)\n' +
    '    at NodePath._call (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/path/context.js:53:20)\n' +
    '    at NodePath.call (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/path/context.js:40:17)\n' +
    '    at NodePath.visit (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/path/context.js:90:31)\n' +
    '    at TraversalContext.visitQueue (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/context.js:103:16)\n' +
    '    at TraversalContext.visitSingle (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/context.js:77:19)\n' +
    '    at TraversalContext.visit (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/context.js:131:19)\n' +
    '    at Function.traverse.node (/Volumes/code/name-of-project/node_modules/@babel/traverse/lib/index.js:82:17)\n' +
    ' @ ./node_modules/axios/lib/defaults.js 1:0-37\n' +
    ' @ ./node_modules/axios/lib/axios.js\n' +
    ' @ ./node_modules/axios/index.js\n' +
    ' @ ./src/web/func/GetDataFromAPI.ts\n' +
    ' @ ./src/web/page/Home.tsx\n' +
    ' @ ./src/web/layout/App.tsx\n' +
    ' @ ./src/web/entry/app.ts'
]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
fivethreeocommented, Sep 23, 2021

I wish next.js babel loader and babel preset was its own packages then razzle could just use those.

0reactions
lyndonbuckleycommented, Oct 2, 2021

Closing this issue.

I ended up ignoring the process module in babel by adding the following modifyWebpackConfig callback

module.exports = ({options: {webpackOptions}, paths}) => {
    if (webpackOptions.babelRule)
        webpackOptions.babelRule.exclude = [`${paths.appNodeModules}/process`];
    return webpackOptions;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript imports not working · Issue #5082 · axios ... - GitHub
I am importing axios as follows into my typescript project: import axios from 'axios';. which compiles without a problem, however the resulting ...
Read more >
I'm not able to use Axios in TypeScript - Stack Overflow
1- First I made index.ts file import axios from "axios"; const url = "https://jsonplaceholder.typicode.com/users/2"; axios.get(url). · 2- Then I ...
Read more >
@jorrmungandr/use-axios - npm
Start using @jorrmungandr/use-axios in your project by running `npm i ... TypeScript icon, indicating that this package has built-in type declarations.
Read more >
Cannot use import statement outside a module [React ...
In this article, you'll learn how to fix the SyntaxError: Cannot use import statement outside a module error when using TypeScript or ...
Read more >
Generating Pdf documents in React Using React-pdf
I was working on a project recently and I got a unique(for me) requirement that needed me to generate pdf file from an...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found