webpack: Parsing of `import()` fails in 4.29.0 (Compilation issue, related to dynamic import)
Bug report
What is the current behavior?
I’ve updated to v4.29.0, and my code is no longer compiling.
Error:
Module parse failed: Unexpected token (11:10)
You may need an appropriate loader to handle this file type.
| var AsyncHome = asyncComponent(appendAsyncReducer, epicSubject$, function () {
| return process.env.SERVER ? require('./home')
> : import(
| /* webpackChunkName: "books" */
| './home');
@ ./server/ssr.js 15:0-34 167:25-34
@ ./server/index.js
If I revert to v4.28.2, it works again.
If the current behavior is a bug, please provide the steps to reproduce.
- Clone
https://github.com/Dbuggerx/react-book-search-sample.git
npm i
and compile the code (npm run start-server
ornpm run start
) - it should work- Now, install webpack v4.29.0 and try to compile again
What is the expected behavior? It should compile the code without errors, as it was doing on v4.28.2
Other relevant information: webpack version: v4.29.0 Node.js version: v10.15.0 Operating System: Fedora linux 29
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 417
- Comments: 171 (24 by maintainers)
Links to this issue
Commits related to this issue
- Lock minor webpack until acorn/acorn-dynamic-import dependency graph works as expected. Related webpack issue (though the fault lies with npm): https://github.com/webpack/webpack/issues/8656 — committed to abcnews/aunty by deleted user 5 years ago
- fix: rollback dependencies to the @vue/cli to keep use of npm seems issue #28 is related to issue webpack/webpack#8656 and the fix is in npm https://npm.community/t/packages-with-peerdependencies-are... — committed to vue-styleguidist/vue-cli-plugin-styleguidist by elevatebart 5 years ago
- fix: downgrade dependency of webpack per issue 8656 webpack/webpack#8656 — committed to vue-styleguidist/vue-styleguidist by elevatebart 5 years ago
- fix: update babelrc config files to the babel.config.js (#260) * fix: update babel and fix all examples * update cypress in tests * add vue to the file looked at by prettier * fix: more DRY ... — committed to vue-styleguidist/vue-styleguidist by elevatebart 5 years ago
- Downgrade webpack to avoid build error See https://github.com/webpack/webpack/issues/8656 — committed to misskey-dev/misskey by syuilo 5 years ago
- Update dependencies Webpack version locked: see https://github.com/webpack/webpack/issues/8656#issuecomment-456713191 — committed to pdavide/discourse-js by pdavide 5 years ago
- fixing webpack npm bug - problem with npm resolution - error message: Module parse failed: 'import' and 'export' may only appear at the top level - see https://github.com/webpack/webpack/issues/8656#... — committed to manGoweb/mango-cli by enzy 5 years ago
- Add Form component that parses Jellyschema A lot of configuration has been updated to be able to support dynamic import syntax, and therefore .wasm files. One interesting change is the explicit depen... — committed to balena-io-modules/rendition by LucianBuzzo 5 years ago
- Add Form component that parses Jellyschema A lot of configuration has been updated to be able to support dynamic import syntax, and therefore .wasm files. One interesting change is the explicit depen... — committed to balena-io-modules/rendition by LucianBuzzo 5 years ago
- fix(gatsby): Pin webpack to fix `gatsby build` problem This addresses #11198. There's a regression bug webpack/webpack#8656 affecting 4.29.0 through at least 4.29.3 which causes errors like this dur... — committed to paulmelnikow/gatsby by paulmelnikow 5 years ago
- fix(gatsby): Pin webpack to fix `gatsby build` problem This addresses #11198. There's a regression bug webpack/webpack#8656 affecting 4.29.0 through at least 4.29.3 which causes errors like this dur... — committed to paulmelnikow/gatsby by paulmelnikow 5 years ago
- fix(gatsby): Pin webpack to fix `gatsby build` problem This addresses #11198. There's a regression bug webpack/webpack#8656 affecting 4.29.0 through at least 4.29.3 which causes errors like this dur... — committed to paulmelnikow/gatsby by paulmelnikow 5 years ago
- fix(gatsby): Pin webpack to fix `gatsby build` problem This addresses #11198. There's a regression bug webpack/webpack#8656 affecting 4.29.0 through at least 4.29.3 which causes errors like this dur... — committed to paulmelnikow/gatsby by paulmelnikow 5 years ago
- fix(gatsby): pin webpack to fix unexpected token error during build (#11640) ## Description This addresses #11198. There's a regression bug webpack/webpack#8656 affecting 4.29.0 through at leas... — committed to gatsbyjs/gatsby by paulmelnikow 5 years ago
- fix(gatsby): pin webpack to fix unexpected token error during build (#11640) ## Description This addresses #11198. There's a regression bug webpack/webpack#8656 affecting 4.29.0 through at least 4.... — committed to gatsbyjs/gatsby-starter-blog by deleted user 5 years ago
- Fix Webpack 4.29 issue with dynamic imports https://github.com/webpack/webpack/issues/8656 -- basically, I had to wipe our package-lock.json and install acorn. Let's nix acorn when we can. — committed to CJWorkbench/cjworkbench by adamhooper 5 years ago
- fix(gatsby): pin webpack to fix unexpected token error during build (#11640) ## Description This addresses #11198. There's a regression bug webpack/webpack#8656 affecting 4.29.0 through at leas... — committed to gurpreet-hanjra/gatsby by paulmelnikow 5 years ago
- fix: https://github.com/webpack/webpack/issues/8656,https://github.com/vuejs/vue-cli/issues/3341 — committed to midwayjs/injection by czy88840616 5 years ago
- docs: auto generate by ci fix: https://github.com/webpack/webpack/issues/8656,https://github.com/vuejs/vue-cli/issues/3341 — committed to midwayjs/injection by deleted user 5 years ago
Running these commands fixes the problem in your repro @Dbuggerx
It seem to be a npm problem.
Here is an explanation:
webpack
depends onacorn@^6.0.5
andacorn-dynamic-import
.acorn-dynamic-import
has apeerDependency
toacorn
. The peer dependency marks thatacorn-dynamic-import
wants to use theacorn
dependency ofwebpack
.Normally this works fine, but in your repo npm created a tree like this:
Note the deduped
acorn@6.0.4
dependency was moved/hoisted to the rootnode_modules
directory and webpack got it’s ownacorn@6.0.5
dependency in it’s ownnode_modules
. So far fine, but npm has chosen to hoist theacorn-dynamic-import
dependency to the rootnode_modules
directory. Now it no longer uses the sameacorn
dependency as webpack does. The version doesn’t matter so much, but the instance equality.I claim that npm should not hoist
acorn-dynamic-import
here as it has apeerDependency
on acorn. It must ensure that webpack and acorn-dynamic-import have access to the sameacorn
dependency.Running the commands above fixes the problem as it pushes all acorn dependency to the same version and allowing to hoist the acorn dependency in webpack too. It’s more a workaround, but should work. It changes the tree to:
same here, downgrading to 4.28 works
The npm bug report got an answer:
Seem like the bug won’t get fixed soon. Here are workarounds to you can use:
"acorn": "^6.0.5"
to your application package.json.yarn
. It doesn’t have this bug.npm update acorn --depth 20
npm dedupe
(works only in some cases)rm package-lock.json
npm i
(works only in some cases)4.28.4
in your package.jsonSame here, works fine when I downgrade to
4.28.4
Steps taken:
It worked for me !!! @sokra
downgrade to
"webpack": "4.28.4"
in your projects package.json also works for now. Just did that a couple of hours ago.Workaround with npm didn’t seem to work in a vue cli project
Steps taken:
Output from npm ls acorn
can confirm downgrade to previous version issue does not persist
$ npm install webpack@4.28.4
these
magic incantationsworkarounds are getting more creative every dayAfter doing what was in https://github.com/webpack/webpack/issues/8656#issuecomment-473418654, I was able to remove the
acorn
dependency and it all still works. Looks like it has to do withpackage-lock.json
.So, in all:
For people still running in this error (found this solution in one of above comments, but removing lock-files was what worked for me):
Install Webpack 4.30.0 (no reason to use older version if this works)
npm i --save-dev acorn@^6.1.1
Make Webpack resolve to newer acorn; add this at end of your
package.json
:node_modules
andpackage-lock.json
andyarn-lock.json
(if using yarn).npm i
.And it should work after this.
Here is an alternative workaround:
add
"acorn": "^6.0.5"
to your applications package.json.@EvanBurbidge I would be interested if using
yarn
solves the issue in your case?Here’s what worked for us in our Ionic v4/Angular app after trying the various work arounds to no avail:
acorn
todevDependencies
.node_modules
.package-lock.json
.npm install
.npm ls acorn
showed thewebpack
dependency onacorn
and everything listed that depended onacorn
had its dependency successfully met.acorn
(npm ls acorn
). The main one that needed updating was@ionic/storage
. After bumping the version, removing theacorn
dev dependency,npm install
, things not only functioned butnpm ls acorn
looked good and@ionic/storage
was no longer listed (for whatever reason).hjjhjhjhjhjhjhjhjh
ghghghghghg
Just in case someone ends up here like I did, here’s the state of affairs (as far as I can tell), to save some scrolling.
My team has had success by removing our package-lock.json and our node_modules folder and re-npm-install-ing, but it’s not clear that that’ll work for everyone (we probably just got lucky with how NPM decided to resolve peer dependencies for us). We’ll probably end up migrating to Yarn, which doesn’t seem to have this problem.
@hai5nguy
“webpack”: “~4.34.0”, “webpack-cli”: “3.3.4”, “acorn”: “^6.1.1”,
rm -rf package-lock.json rm -rf node_modules npm i
worked for me
Reverting the change would be a breaking change for users that are not affected by the npm bug.
I would prefer to fix the bug in
npm
, so I’m working on a PR to npm cli: https://github.com/npm/cli/pull/147Worth mentioning this doesn’t manifest when installing packages using
yarn
(at least for@vue/cli
).https://npm.community/t/release-npm-6-8-0-next-0/5058
There is a beta npm version including the fix.
Thanks for the explanation @sokra! I can’t try the workaround right now (will do it as soon as I get home and will let you know). But since I’m not the only one with such issue, how could this “npm issue” be avoided? I mean, as a webpack user, I expected to install the new version and keep working normally.
this is a major issue, why isnt that already fixed ?
I did
"webpack": "^4.41.5",
it solves the issue for me. The actual webpack version installed is4.43.0
.Confirmed solution that worked for me:
"webpack": "~4.34.0",
"webpack-cli": "3.3.4",
"acorn": "^6.1.1",
rm -rf package-lock.json
rm -rf node_modules
npm i
Also reported here: https://npm.community/t/packages-with-peerdependencies-are-incorrectly-hoisted/4794
Due to acorn plugin is now integrated into acorn itself, this npm issue is no longer an issue for webpack. Note that the npm problem is still not solved and could re-occur anytime again.
@sokra works with yarn.
When installing acorn still no joy with npm. Getting the following warning.
npm WARN acorn-dynamic-import@4.0.0 requires a peer of acorn@^6.0.0 but none is installed. You must install peer dependencies yourself.
Steps Taken:
this bug doesnt exist anymore as 4.36.0, try : “webpack”: “4.36.0”, “webpack-cli”: “3.3.6”, “webpack-dev-server”: “3.7.2”
You can upvote this PR if you want to show your interest in the fix: https://github.com/npm/cli/pull/147
@sokra I don’t suppose it’s your intention to keep confirmed breaking changes in minor or patch versions, so are you considering making a minor/patch version where you revert the breaking change (acorn v6), so we’re not forced to pin our version of webpack to 4.28.4?
my bad, did a clean install but forgot to delete package-lock.json. after cleaning that it indeed works,
It is work.
I was encountering the same issue even after trying the above-mentioned steps. The following did the trick for me to resolve the issue for my setup which has babel and webpack.
In addition to performing the steps mentioned in https://github.com/webpack/webpack/issues/8656#issuecomment-456086484 Try the following if you are using Babel and Webpack:
npm i -D babel-plugin-dynamic-import-node
dynamic-import-node
option in babel configurationNote: I’m using babel 7.3.0 with webpack
If its still doesnt work then going back to webpack 4.28.x is the last option
that’s less of a solution and more of a workaround. and i only want to call it out as a workaround because it feels like the kind of cruft that quickly becomes tech debt. in a few days or weeks you’ll be asking yourself why did you do this non-standard thing. if you know the why you’ll still be asking if you’ve implemented it in all the necessary places. and if you have implemented it, then you’ll have to wonder if the workaround is still necessary. and if it is, then you’ll ask what timeline you should expect to move away from it 🙄
the core issue is still peer dependency resolution within npm itself, and until that’s resolved, there’s really no solution 😞
the other workarounds are downgrade webpack, use yarn, install acorn as a direct dependency, or use the npm release candidate that had the fix before it was reverted.
Adding
acorn@^6.0.7
to the project’s dependencies, removingpackage-lock.json
andnode_modules
and then reinstalling did the trick for me. 😃o my god, i spend many hours on this problem TTTTTTT
Fix is scheduled for next npm release.
Yes
No, I don’t see a reason why someone should be unable to update their npm version to upgrade webpack. Note that afaik you only need to latest npm version to upgrade webpack. This should result in a correct lockfile which is installable with older version. As long as you don’t run
npm dedupe
which an old version you are fine.But I’ll add a note to the release notes.
Wasted my working day trying to figure out why it’s because of the new version thx 😩
@PerspectivesLab It’s actually as of
4.35.3
:That also worked for me. After adding the latest
acorn
todevDependencies,
you would either need to delete the package.lock.json or runnpm dedupe
afternpm i
.I had to include the
"acorn": "6.1.1"
intodevDependencies
with Webpack 4.29.0 and NPM. Then you have to delete the lock file or runnpm dedupe
for the Webpack not to fail.FWIW I had the same problem, but it worked fine locally on Mac but failed on Jenkins (with version 4.29), reverting to 4.28.4 fixed it 👍
aye, it works like this, but that’s not optimal, imo, since we don’t use the acorn dep in the project explicitly, so there shouldn’t be a need to define the dep in package.json
This helped a lot, @devCrossNet
Installing acorn@6.1.0 and acorn-dynamic-import@4.0.0 worked for me.
npm i --save-dev acorn@6.1.0 acorn-dynamic-import@4.0.0
Thanks!!!
❤️ I also got this issue today, probably after upgrading packages. I lost a few hours trying to figure out what the hell has happened…
Adding
"acorn": "^6.0.7"
topackage.json
solved this issue for me when usingyarn
without downgradingwebpack
(I am using4.29.0
).Note: It’s hard to find a solution to this issue, as searching leads to older discussions about
@babel/syntax-dynamic-import
issues (or anything related to import() being a proposal API) that were already solved in late 2018.is there any solution yet for this (aside from downgrading)?
Same issue here. All calls to
import()
result in anUnexpected token
Error.@dushyantec71 the backscroll is helpful here. This isn’t a problem with Webpack, but with NPM. You can either try and shuffle around your
node_modules
directory or you can migrate to yarn — unfortunately there aren’t any other fixes. (I’m not a Webpack maintainer)Solved (for me)
Hopefully this will work for others as well. My dev team mate and I was able to fix my issue based on the explanation from @sokra.
In short: the order that the dependencies are installed, matter.
Here is my acorn dependency tree:
The problem occurred when I had to start working from home and I pulled the repo onto my local machine. All of that was fine, but I figured out later that in this case, it was the order that the npm packages were installed made all the difference.
At work, the
jspdf
package was installed at a later time, after all the other packages had been installed, so at that point, the version ofacorn
was6.0.5
. So when I installedjspdf
with the dependency ofacorn
version 2.7.0, it just used version 6.0.5.But when I installed all the packages at home,
jspdf
was one of the first packages installed that usedacorn
, and it install version 2.7.0. So any other package that depended onacorn
, used version 2.7.0 which would break that package.The Solution
To solve the problem, I did the following:
node_modules
folder and thepackage-lock.json
file.jspdf
reference from thepackage.json
file.npm install
to install all packages exceptjspdf
jspdf
So the solution for me was to remove references for any packages that may be the cause of the conflict, install everything else first, then install those other needed packages. This way the order of the dependencies that all packages need are using the best possible versions of the package.
Hopefully this will work for others.
good catch, i also had trouble with 4.35.3 like @tzetzo, but the update to 4.36.0 definitely fixed all of it, @tzetzo could you update to 4.36.0 and let us know if it fixes your issue
https://github.com/acornjs/acorn/issues/824#issuecomment-508368092
@rbasniak Try adding
"acorn": "^6.1.1"
to the"dependencies"
in yourpackage.json
, and then donpm install
.Alternatively, you can switch to
yarn
.upgrading to 10 won’t work, there is a bug in npm. you just need to add latest version of
acorn
to your package.json. As far as I can tell, deps won’t be upgraded for webpack 4, all resources to 5 (correct me if I’m wrong)Is there a fix for this that doesn’t rely on downgrading to
4.28.2
? There’s a feature in4.32.0
that I’d like to take advantage of and any help would be much appreciated.Running the following commands didn’t fix the issue for me
Running
npm ls acorn
returnsAddings of dynamic-import-node-babel-7 plugin works fine for the latest version of wedkpack. For react you can additionally add react-loadable/babel as well.
in webpack.config.js add
plugins: [ "@babel/plugin-proposal-class-properties", "@babel/syntax-dynamic-import", // For code splitting "react-loadable/babel", "dynamic-import-node-babel-7", ],
@JoshRobertson I saw that too and am still seeing the mentioned error with both “@babel/parser” and “acorn” resolutions.
Already spent some hours debugging the problem. I’ll try to create a minimal reproduction.
@bogem
I believe that is a regression in the @babel/parser in ^7.40 : https://github.com/babel/babel/issues/9763
I’ve used a yarn resolution to avoid the latest version:
@babel/parser": "7.3.4"
@korya yeah, using yarn is a viable workaround. adding acorn as a direct dependency is another one.
It worked for me, too.
@chandan-singh That’s for running in a Node environment, to transpile
import
torequire
. In Webpack you’d want, dynamic import syntax, because Wepback will use internal Promises for the dynamic imports.@mvirbicianskas contributing here means we’d like to know how the workarounds work for you also. 🙂
Thanks for your work @sokra
@carrillo455 yes, you can blow away your lockfile or scroll up and you will see a fix.
npm update acorn --depth 20 npm dedupe
WHY DID YOU MARK THIS AS SPAM?! I’m serious, I spent 2 days banging my head against the wall trying to understand how this upgrade wasn’t working when the upgrade I did on a different repo a few months back worked just fine… Obviously, you aren’t wanting anymore embarrassment about how badly this breaks some users
adding “acorn”: “^6.0.5” to package json didn’t work for me, still same error only 4.28.4 works
Hey @sokra, I’ve ended up deleting package-lock.json and node_modules, then
npm i
and it’s working again! I’ve also experimented reverting this fix, and triedyarn
, which also fixes the issue as well. Thanks for the detailed information!Downgrading did work from 4.30.0 to 4.28.4, but upgrading to 4.35.3 also works.
we used lerna and came across this problem also, in the end we added
"acorn": "6.1.1",
as dependency and it worked with"webpack": "^4.30.0",
hope it helps
Going to link back to the post by @KevinKelchen as a workaround since it’s pretty buried now
https://github.com/webpack/webpack/issues/8656#issuecomment-473418654
Your problem is not with dynamic import. It is the issue of less or scss loader (or whatever script you are using). Please add a proper loader for your code.
@Jaskaranbir Yeah that worked for me. 🙏
A similar issue exists with Typescript exports where switching to Yarn does NOT solve the problem. Exporting a TS interface errors (while the export of the module does not).
Yarn 1.15.2 Webpack 4.29.6 Typescript 3.4
generates
pinning
acorn
to 6.0.5 with yarn resolutions is required for this code to compile:Thanks, @KevinKelchen. That solved the problem. Initially I thought maybe I had a broken change show up. I was going through all of my config files trying to solve it.
Had this issue as well. @KevinKelchen – Your solution worked for me too.
Another solution may be to update all dependencies relying on
acorn
so that they depend on the latest (6.x) version. For my project, it was onlywebpack
andwebpack-bundle-analyzer
(which has recently been updated).Run
npm ls acorn
.This is my output after updating both:
A simple fix for this to remove package-lock.json & node modules. And then run
npm install
. In my case, that problem was coming because of updation of acorn dependency.terrible