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

  1. Create a Next app with appDir enabled:
image
$ 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
  1. Run pnpm dev --turbo. It throws.

Turbopack

image image

Webpack

image

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

Most upvoted comments

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.2 to temporarily undo the breaking PR

I 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 next project 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.

import { AsyncLocalStorage } from "node:async_hooks";
import { createRequire } from "module";

// Initialize AsyncLocalStorage before anything else
const als = new AsyncLocalStorage();

// Set up AsyncLocalStorage in the global scope
Object.defineProperty(globalThis, "AsyncLocalStorage", {
    value: AsyncLocalStorage,
    configurable: false,
    writable: false,
    enumerable: true,
});

Object.defineProperty(globalThis, "asyncLocalStorage", {
    value: als,
    configurable: false,
    writable: false,
    enumerable: true,
});

// Create a require function
const require = createRequire(import.meta.url);

// Get Next.js's module cache
const moduleCache = require.cache;

// Delete Next.js's async-local-storage module if it's already cached
Object.keys(moduleCache).forEach((key) => {
    if (key.includes("next/dist/server/app-render/async-local-storage")) {
        // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
        delete moduleCache[key];
    }
});

// Export the AsyncLocalStorage instance for use in other modules
export { als as asyncLocalStorage, AsyncLocalStorage };

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:

  1. Our server code was running in ESM mode
  2. Next.js’s internal code was running in CommonJS mode
  3. When Next.js tried to access AsyncLocalStorage in its app-render code, it was looking in its own module context, not finding our global setup

The error specifically occurred because:

  1. Next.js’s app-render/async-local-storage.js was trying to access AsyncLocalStorage
  2. This module was being loaded through Next.js’s CommonJS module loader
  3. The AsyncLocalStorage we set up in our ESM context wasn’t visible to Next.js’s CommonJS modules

The solution was to:

  1. Set up AsyncLocalStorage before any Next.js code runs
  2. Use globalThis instead of global to ensure cross-context visibility
  3. Make the properties non-configurable and non-writable to prevent accidental modifications
  4. Move this setup to a dedicated file that runs first

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