Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Unexpected token when importing axios -- node-resolve should be first #66

Closed
JWMB opened this issue Mar 19, 2018 · 8 comments
Closed
Labels
problem: plugin order The plugin order in this issue seems incompatible. See the "Compatibility" section in the README scope: docs Documentation could be improved. Or changes that only affect docs scope: integration Related to an integration, not necessarily to core (but could influence core) scope: upstream Issue in upstream dependency solution: out-of-scope This is out of scope for this project solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin

Comments

@JWMB
Copy link

JWMB commented Mar 19, 2018

[!] 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()
	]
};
@ezolenko
Copy link
Owner

ezolenko commented Mar 20, 2018

Could you try suggestion made here: rollup/rollup-plugin-commonjs#28 (using rollup-plugin-json)

@JWMB
Copy link
Author

JWMB commented Mar 21, 2018

Thanks, that seems to work (adding rollup-plugin-json as a plugin)!

The output is very different from rolling up the corresponding .js file though, with the iife signature becoming

var SomeName = (function (exports,tty,util,url,http,https,assert,stream,zlib)...

I was expecting rollup-plugin-typescript2 to basically run tsc with my tsconfig.config as a pre-rollup step, but when I do that (tsc first, then rollup without the typescript plugin) I get

var SomeName = (function (exports)...

I'm probably missing some config setting here..?

@ezolenko
Copy link
Owner

Are those other parameters used inside the function at all? Is the body the same in both cases?

@JWMB
Copy link
Author

JWMB commented Mar 22, 2018

Yes, they are used, and there's an additional big chunk, starting with

 function createCommonjsModule(fn, module) {

in the middle of the body (when using the typescript plugin compared to using plain js as the source). A lot of it seems related to Node debugging functionality.
I've put up a minimal test case repo here.

@ezolenko
Copy link
Owner

ezolenko commented Apr 4, 2018

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.

ezolenko added a commit that referenced this issue Apr 4, 2018
@ezolenko
Copy link
Owner

ezolenko commented Apr 4, 2018

You probably don't need json plugin now either.

@giannif
Copy link

giannif commented Jan 15, 2019

@ezolenko I was really thrown off by the readme 😬

It says:

rollup-plugin-node-resolve
Must be before this plugin in the plugin list, especially when browser: true option is used, see #66

I think I was thinking where do I put the typescript plugin, and then I read the next line and thought this plugin was referring to rollup-plugin-node-resolve.

Great work though! Thanks

ezolenko added a commit that referenced this issue Jan 15, 2019
@ezolenko
Copy link
Owner

Fixed, thanks :)

@agilgur5 agilgur5 changed the title Error: Unexpected token (axios import) Error: Unexpected token when importing axios May 22, 2022
@agilgur5 agilgur5 added scope: integration Related to an integration, not necessarily to core (but could influence core) solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin solution: out-of-scope This is out of scope for this project scope: docs Documentation could be improved. Or changes that only affect docs scope: upstream Issue in upstream dependency problem: plugin order The plugin order in this issue seems incompatible. See the "Compatibility" section in the README labels May 22, 2022
Repository owner locked as resolved and limited conversation to collaborators May 22, 2022
@agilgur5 agilgur5 changed the title Error: Unexpected token when importing axios Error: Unexpected token when importing axios -- node-resolve should be first May 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
problem: plugin order The plugin order in this issue seems incompatible. See the "Compatibility" section in the README scope: docs Documentation could be improved. Or changes that only affect docs scope: integration Related to an integration, not necessarily to core (but could influence core) scope: upstream Issue in upstream dependency solution: out-of-scope This is out of scope for this project solution: tsc behavior This is tsc's behavior as well, so this is not a bug with this plugin
Projects
None yet
Development

No branches or pull requests

4 participants