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.

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:closed
  • Created 6 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
ezolenkocommented, Jan 15, 2019

Fixed, thanks 😃

3reactions
ezolenkocommented, May 22, 2022

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 for code 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 grabs axios/dist/axios.js – webpacked build. In case of using the plugin axios/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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

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