next.js: 8.0.0 - dynamic import causing 'You may need an appropriate loader...' error

Bug report

Just upgraded to 8.0.0 and I’m really excited! I’ve run into an issue though that is preventing my project from compiling

Describe the bug

So when I run dev I am getting the following issue

You may need an appropriate loader to handle this file type.
| 
| 
> import('./noop');
| var _window = window,
|     assetPrefix = _window.__NEXT_DATA__.assetPrefix;

It seems to be coming from webpack 4.29.0 (see here).

It looks like the issue was fixed with this which declared acorn": "^6.0.5 as a dependency but the change doesn’t seem to be present on master

Maybe I upgraded too soon?

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 92
  • Comments: 59 (18 by maintainers)

Commits related to this issue

Most upvoted comments

Ok here is the winning combo (made sure is 100% reproducible): My project already had next v7

  1. npm install next@latest -> installs next v8.0.1 but warns about acorn
  2. npm run dev -> runs next and fails with the error described in this git issue
  3. npm install acorn
  4. npm run dev -> runs next and fails with the error described in this git issue
  5. npm install next@latest -> no warnings
  6. npm run dev it works!!! 🎉

Conclusion The key is to install acorn before next

We are also seeing this issue and followed the threads for a workaround.

  • Production (with next build) builds and runs fine.
  • Running in development mode produces the same error as OP.
  • We invoke Next.js programmatically (we use it with Express currently).
  • Updating @babel/core to 7.2.2 did not resolve this for us.

Following the the issues linked in the thread and installing webpack@4.28.4 and @babel/core@7.2.2 as devDependancies did resolve this.

(This also resolved the error about the Acorn package, which we do not have as a direct dependancy.)

I had the same issue, also after updating webpack and @babel/core. Removing my package-lock.json and node_modules and doing a fresh install solved the issue.

Deleting node_modules and package-lock.json didn’t resolve this for me, but installing webpack@4.28.4 as a dev dependency did, as @iaincollins suggested.

Using Yarn to install dependencies did the trick for me. Super weird.

@Timer I’m running next@9.0.4 and still having the issue. I’ve tried:

  1. removing next, adding acorn with yarn add acorn and then running yarn next@latest to no avail
  2. removing all node_modules and yarn.lock and reinstalling. Anybody else still seeing this issue? I’ve only begun to experience it with react-datepicker

@timneutkens I finally discovered last night that my issue was caused by #6273. As I mentioned in that thread, the error goes away if I modify my next/babel preset to ['next/babel', { 'transform-runtime': { useESModules: false } }].

EDIT: I just published a test repo, see discussion in #6273.

I have the same behaviour in TypeScript files:

error  in ./stores/index.ts

Module parse failed: Export 'IStore' is not defined (77:9)
You may need an appropriate loader to handle this file type.
|   return Store;
| };
> export { IStore, IStoreSnapshotIn, IStoreSnapshotOut };

 @ ./pages/_app.tsx 16:0-37 38:24-32 83:18-26

I have a different error related to imports which might have the same root cause:

/Users/andrewingram/Repositories/tego/website-next/node_modules/@babel/runtime-corejs2/helpers/esm/typeof.js:1
(function (exports, require, module, __filename, __dirname) { import _Symbol$iterator from "../../core-js/symbol/iterator";
                                                                      ^^^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier

Everything is fine if I downgrade to next@7.0.3

Ok here is the winning combo (made sure is 100% reproducible): My project already had next v7

  • npm install next@latest -> installs next v8.0.1 but warns about acorn
  • npm run dev -> runs next and fails with the error described in this git issue
  • npm install acorn
  • npm run dev -> runs next and fails with the error described in this git issue
  • npm install next@latest -> no warnings
  • npm run dev it works!!! tada

Conclusion: The key is to install acorn before next

This fixed the issue for me too, thanks ! 👏

Same as @Soundvessel

Installing acorn prior next@8 fixed the issue.

npm ci
npm i -S acorn@latest
npm i -S next@latest

This happens for me directly between v8.0.0-canary.11 and v8.0.0-canary.12 https://github.com/zeit/next.js/compare/v8.0.0-canary.11...v8.0.0-canary.12

Installing webpack@4.28.4 and @babel/core@7.2.2 as devDependencies as @iaincollins suggested fixes this for me as well.

So was it some kind of dependency resolution? @vasco3 that solved my problem.

Removing node_modules and package-lock.json then reinstalling resolved this for me as well.

I had the same issue, also after updating webpack and @babel/core. Removing my package-lock.json and node_modules and doing a fresh install solved the issue.

Thanks, this solution solved the issue for me. I had been working on another Node.js project (not Next.js) where I’d upgraded my version of Node to the latest - this seems to be the cause of the issue. Where the solution to remove the lock.json and node_modules, then re-install the packages worked perfectly. Thanks.

The final resolution steps should be to rm package-lock.json and rm -rf node_modules, then reinstall.

It’s important to note that this is only fixed in Next 9.

This doesn’t seem to fix the issue for me. Dealing with an older code base that can’t quite upgrade to the new features yet, and i still need a transpiled server.

I’ve done the above steps, and they don’t solve the issue. I’m not even using dynamic imports 😕

Compiling client app. 
Compiling server app. 
[ error ] ./node_modules/next/dist/client/next-dev.js 23:6
Module parse failed: Unexpected token (23:6)
You may need an appropriate loader to handle this file type.
| 
| 
> import('./dev/noop'); // Support EventSource on Internet Explorer 11
| 
| if (!window.EventSource) {
[ wait ]  compiling ...
[ error ] ./node_modules/next/dist/client/next-dev.js 23:6
Module parse failed: Unexpected token (23:6)
You may need an appropriate loader to handle this file type.
| 
| 
> import('./dev/noop'); // Support EventSource on Internet Explorer 11
| 
| if (!window.EventSource) {
2019-07-13T08:07:59.942Z app:server:log Connected to redis
2019-07-13T08:07:59.947Z app:server:log > Ready on http://localhost:3000

Ok here is the winning combo (made sure is 100% reproducible): My project already had next v7

* `npm install next@latest` -> installs next v8.0.1 but warns about acorn

* `npm run dev` -> runs next and fails with the error described in this git issue

* `npm install acorn`

* `npm run dev` -> runs next and fails with the error described in this git issue

* `npm install next@latest` -> no warnings

* `npm run dev` it works!!! 🎉

Conclusion: The key is to install acorn before next

Thanks a ton, this fixed the problem. 👏👏

Ok here is the winning combo (made sure is 100% reproducible): My project already had next v7

  • npm install next@latest -> installs next v8.0.1 but warns about acorn
  • npm run dev -> runs next and fails with the error described in this git issue
  • npm install acorn
  • npm run dev -> runs next and fails with the error described in this git issue
  • npm install next@latest -> no warnings
  • npm run dev it works!!! 🎉

Conclusion: The key is to install acorn before next

thanks! proves once again that order matters! saved me.

I used Yarn to install dependencies too and it works)

To clarify I did not need Acorn to resolve the issue. Try to uninstall Next using the command line instead of just deleting node_modules.

npm uninstall --save next
npm install --save next@latest

I believe that using the command line to uninstall Next removes the cause of trouble from an NPM cache outside of node_modules.

I also updated Node and NPM beforehand but that didn’t help until I properly reinstalled.

Got 8.0.3 running without Acorn.

It appears NPM was caching next incorrectly. Even when deleting my node_modules folder and package-lock rerunning npm i the issue would persist. Actually using the NPM CLI to uninstall next appears to have removed it from the cache properly and after a reinstall without Acorn it is up and running.

I ran in to the same issue. Removing both node_modules and package-lock.json and reinstalling did fix it for me. It’s important to remove both the directory and lock file before reinstalling, just in case that’s the issue some are hitting.

I have a different error related to imports which might have the same root cause:

/Users/andrewingram/Repositories/tego/website-next/node_modules/@babel/runtime-corejs2/helpers/esm/typeof.js:1
(function (exports, require, module, __filename, __dirname) { import _Symbol$iterator from "../../core-js/symbol/iterator";
                                                                      ^^^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier

Everything is fine if I downgrade to next@7.0.3

Facing the same issue as @AndrewIngram on a complex project. The issue only appears for next@8.0.0 in dev mode, not when compiling for production.

For me this was fixed by upgrading @babel/core:

-        "@babel/core": "^7.1.0",
+        "@babel/core": "7.2.2",

For some reason we have that as direct dependency currently.

I’m facing the same error. Installing acorn doesn’t work for me either. FYI, I use ts-node for running my express server.