Error: `Unexpected token` when importing axios -- `node-resolve` should be first
See original GitHub issue[!] Error: Unexpected token
node_modules\axios\package.json (2:9)
1: {
2: "_from": "axios",
^
Same error as when importing axios into pure ES6 project and not having set browser: true
for rollup-plugin-node-resolve
. Simple test case: single js file containing
import axios from "axios";
window.process = window.process || { env: {} };
axios.get("http://httpbin.org/").then(response => console.log("Got length: " + response.data.length));
Using the below rollup.config.js produces the error, but when excluding rollup-plugin-typescript2 it works.
Versions
- node: 8.9.4
- typescript: 2.7.2
- rollup: 0.57.1
- rollup-plugin-typescript2: 0.12.0
rollup.config.js
import typescript from 'rollup-plugin-typescript2';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
export default {
input: 'src/main.js',
output: {
file: 'public/bundle.js',
format: 'iife',
sourcemap: true
},
plugins: [
typescript(),
resolve({ browser: true }),
commonjs()
]
};
Issue Analytics
- State:
- Created 6 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
import axios from axios - Parsing error: Unexpected token
When trying to import axios from axios getting syntax error: "ESLint: Parsing error: Unexpected token". When used npm install axios I got ...
Read more >failed to resolve module specifier "axios" - You.com
I was able to find the solution for my own problem :) Since I could not import axios in the index.js file I...
Read more >@rollup/plugin-commonjs - npm
If you are using @rollup/plugin-node-resolve , it should be v13.0.6+. ... import commonjs from '@rollup/plugin-commonjs'; export default ...
Read more >How to Use Svelte and Go to Build a Video Chat App - Twilio
In this tutorial, you will learn how to build a video chat app with Twilio ... In the absence of any errors, the...
Read more >Getting an axios error when importing a library/module - Reddit
I have a module that is a js file that gives me this error: Unexpected token (Note that you need @rollup/plugin-json to import...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Fixed, thanks 😃
Looking at your test repo, the plugin produces the same output tsc does. You can check for it in
.rpt2_cache/xxx/code/xxx
file – look forcode
property of json inside. That is what gets passed to rollup.The main difference in 2 results is when
import axios from 'axios';
does – in the cases of plain js rollup grabsaxios/dist/axios.js
– webpacked build. In case of using the pluginaxios/lib/axios.js
is resolved.Webpacked build is apparently the browser version that get magically found by
rollup-plugin-node-resolve
in browser mode.If you put typescript plugin last in the chain, rollup uses
node-resolve
to resolve imports by itself, and everything is working as expected.This line in your test repo:
tsPluginConf.plugins.splice(tsPluginConf.plugins.length - 1, 0, typescript());
This is the first time I’ve seen plugin order actually matter 😃. I’ll update readme.