turborepo: [turbopack] Invariant: AsyncLocalStorage accessed in runtime where it is not available
What version of Turbopack are you using?
next 13.1.2
What package manager are you using / does the bug impact?
pnpm
What operating system are you using?
Mac
Describe the Bug
Turbopack throws the runtime error Invariant: AsyncLocalStorage accessed in runtime where it is not available when attempting to start up a Next.js project with appDir: true. Webpack works as expected.
Expected Behavior
Turbopack should works as what Webpack does.
To Reproduce
- Create a Next app with
appDirenabled:
$ pnpm create next-app@latest
../.pnpm-store/v3/tmp/dlx-90629 | +1 +
Packages are hard linked from the content-addressable store to the virtual store.
Content-addressable store is at: /Volumes/Dev/.pnpm-store/v3
Virtual store is at: ../.pnpm-store/v3/tmp/dlx-90629/node_modules/.pnpm
../.pnpm-store/v3/tmp/dlx-90629 | Progress: resolved 1, reused 0, downloaded 1, added 1, done
✔ What is your project named? … experiments
✔ Would you like to use TypeScript with this project? … No / [Yes]
✔ Would you like to use ESLint with this project? … No / [Yes]
✔ Would you like to use `src/` directory with this project? … No / [Yes]
✔ Would you like to use experimental `app/` directory with this project? … No / [Yes]
Creating a new Next.js app in /Volumes/Dev/Experiments/experiments.
Using pnpm.
Installing dependencies:
- react
- react-dom
- next
- @next/font
- typescript
- @types/react
- @types/node
- @types/react-dom
- eslint
- eslint-config-next
Packages: +267
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Packages are hard linked from the content-addressable store to the virtual store.
Content-addressable store is at: /Volumes/Dev/.pnpm-store/v3
Virtual store is at: node_modules/.pnpm
dependencies:
+ @next/font 13.1.2
+ @types/node 18.11.18
+ @types/react 18.0.26
+ @types/react-dom 18.0.10
+ eslint 8.32.0
+ eslint-config-next 13.1.2
+ next 13.1.2
+ react 18.2.0
+ react-dom 18.2.0
+ typescript 4.9.4
Progress: resolved 279, reused 267, downloaded 0, added 267, done
Done in 15.2s
Initializing project with template: app
Initialized a git repository.
Success! Created experiments at /Volumes/Dev/Experiments/experiments
- Run
pnpm dev --turbo. It throws.
Turbopack
Webpack
Reproduction Repo
No response
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 15
- Comments: 20 (2 by maintainers)
Commits related to this issue
- Install AsyncLocalStorage for appDir rendering (#3374) https://github.com/vercel/next.js/pull/44668 refactored Next's use of `AsyncLocalStorage`, and installs a "polyfill" of the API by patching the ... — committed to vercel/turborepo by jridgewell 4 years ago
- Install AsyncLocalStorage for appDir rendering (vercel/turbo#3374) https://github.com/vercel/next.js/pull/44668 refactored Next's use of `AsyncLocalStorage`, and installs a "polyfill" of the API by p... — committed to vercel/next.js by jridgewell 4 years ago
- Install AsyncLocalStorage for appDir rendering (vercel/turbo#3374) https://github.com/vercel/next.js/pull/44668 refactored Next's use of `AsyncLocalStorage`, and installs a "polyfill" of the API by p... — committed to vercel/next.js by jridgewell 4 years ago
- Install AsyncLocalStorage for appDir rendering (vercel/turbo#3374) https://github.com/vercel/next.js/pull/44668 refactored Next's use of `AsyncLocalStorage`, and installs a "polyfill" of the API by p... — committed to vercel/next.js by jridgewell 4 years ago
- Install AsyncLocalStorage for appDir rendering (vercel/turbo#3374) https://github.com/vercel/next.js/pull/44668 refactored Next's use of `AsyncLocalStorage`, and installs a "polyfill" of the API by p... — committed to vercel/next.js by jridgewell 4 years ago
- Install AsyncLocalStorage for appDir rendering (vercel/turbo#3374) https://github.com/vercel/next.js/pull/44668 refactored Next's use of `AsyncLocalStorage`, and installs a "polyfill" of the API by p... — committed to vercel/next.js by jridgewell 4 years ago
I have the same issue
Hi all, I’ll be addressing this today. It’s likely we’ll need a new Next.js canary release in order for this to take effect. In the meantime, you should be able to
npm install next@13.1.2-canary.2to temporarily undo the breaking PRI have the same issue, started facing it today so probably its a turbopack bug
I still see it with 15.2.3, using turbopack. For me it had nothing to do with turbopack (I think). I am running my
nextproject from a separate that initialized a custom Next server.I ended up saving the following code in a file (e.g.
asyncLocalStorageSetup.ts), and making that the first import statement of the file that is executed.Explanation from Claude:
The root cause of the AsyncLocalStorage error was a conflict between ESM (ECMAScript Modules) and CommonJS module systems in the production environment. Here’s what was happening:
The error specifically occurred because:
The solution was to:
This is why we created
asyncLocalStorageSetup.js- it ensures AsyncLocalStorage is properly initialized in a way that’s visible to both ESM and CommonJS modules before Next.js starts trying to use it. The error only appeared in production because development mode handles module loading differently and has more forgiving module boundary checks.change the ‘next’ to 13.1.0 in package.json and pnpm i