next.js: Module not found: Can't resolve '#async_hooks'. Getting error with modules using sub-path imports

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/sharp-spence-fyy83f

To Reproduce

The codesandbox provided displays the error message when running the application.

./node_modules/p-limit/index.js (2:0) Module not found: Can’t resolve ‘#async_hooks’ Did you mean ‘./#async_hooks’? Requests that should resolve in the current directory need to start with ‘./’. Requests that start with a name are treated as module requests and resolve within module directories (node_modules, /workspace). If changing the source code is not an option there is also a resolve options called ‘preferRelative’ which tries to resolve these kind of requests in the current directory too. https://nextjs.org/docs/messages/module-not-found Import trace for requested module: ./app/page.tsx

Explanation:

In one of my projects I am using p-limit. The last update of this dependency, version 5.0.0 takes advantage of sub-path imports, which apparently NextJS 14 does not handle very well, and reports the error above when compiling the page.

Current vs. Expected behavior

Expected behaviour:

No error should be raised with the module p-limit used in a server component.

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP PREEMPT_DYNAMIC Sun Aug  6 20:05:33 UTC 2023
Binaries:
  Node: 20.8.1
  npm: 10.1.0
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 14.0.1
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Not sure

Additional context

I did not actually try with canary on codesandbox because when canary, the appDir was said to be experimental. So the example given is with version 14.0.1 of NextJS.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Reactions: 11
  • Comments: 15 (3 by maintainers)

Most upvoted comments

I’m suddenly getting this issue as well (using cloudflare next-on-pages)…

Is there a solution?

The crutch is p-limit uses subpath imports in its package.json, to conditionally load async_hooks with a stub outside of node environments. Possibly the nextjs bundler (turbopack and/or webpack) is being confused there, because, as far as package structure concerned, it should “work” as in not crash at module resolution time.

It is working @quy-devbase - Thanks so much.

For reference I solved this from failing in jest by having the following jest config: (this is inside my package.json)

 "jest": {
    "moduleNameMapper": {
         "^#async_hooks": "<rootDir>/node_modules/p-limit/async-hooks-stub.js"
    },
    "transformIgnorePatterns": [
      "<rootDir>/node_modules/(?!yocto-queue|p-limit)"
    ]
  },

I have the same error with App Router structure. I’ve p-limit in my route.ts: Capture d’écran 2024-03-18 à 09 49 28

Any update ?

I’ve check closed issue on p-limit repo: https://github.com/sindresorhus/p-limit/issues/72

The problem is solved if i use p-limit@4.0.0. Only crash on version 5.