babel-loader: Unexpected token when using import()
I’m submitting a bug report
Webpack Version: 3.3.0
Babel Core Version: 6.25.0
Babel Loader Version: 7.1.1
Please tell us about your environment: Linux
Current behavior:
Using import
function result in
Module build failed: SyntaxError: Unexpected token, expected {
Expected/desired behavior: Letting webpack handle this.
I read somewhere modules should be disabled for babel, if that’s the case I did not found how. Any insight?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 5
- Comments: 20 (2 by maintainers)
I have enabled that plugin in .babelrc but still get the error. Any idea?
You’ll need to enable https://babeljs.io/docs/plugins/syntax-dynamic-import/ The
import()
syntax is still an experimental proposal, so you need to opt-in to Babel’s support for it.@felixfbecker Turns out you need babel-plugin-dynamic-import-node It fixed the issue for me - run it in before you run tests. However, It will cause Webpack to create a single bundle, so make sure you only use that plugin for tests, and not for your build.
@felixfbecker, I just came across the very same issue and adding this plugin fixed it. Make sure you add the property “plugins”: [“syntax-dynamic-import”] like this:
I had the same problem. I tried
@babel/plugin-syntax-dynamic-import
andbabel-plugin-dynamic-import-node
but didn’t work. So after a couple of hours, I found out that the problem was my 2 configuration files (.babelrc
andwebpack.config.js
). Those configuration files were merging and the result wasn’t the expected.Solution: Handle your webpack and babel configuration in a single file. I did it on my
webpack.config.js
file as follows:@felixfbecker do you use SSR ?
It got solved for me when I changed the webpack’s file name from
webpack.config.js
towebpack.config.babel.js
issue still exists
webpack.dev.config
error
any babels plugins do not help
@komali2 yep, it should. we’ll need a bit more info to debug though… can you create a new issue with your configs (or even better, a small repo that repros the issue)?
I am having this issue, none of the above solutions function / are ideal.
Does webpack dynamic importing work with babel?
// try this util i made to avoid issues related to dynamic imports with ssr rendering
how to use?
getting
.babelrc
dependencies: