nextjs - 💡(How to fix) Fix sharedAsyncLocalStorageNotAvailableError on 14 -> 15 upgrade [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86719Fetched 2026-04-08 02:09:34
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×3commented ×1issue_type_added ×1

Error Message

Error: Invariant: AsyncLocalStorage accessed in runtime where it is not available at Object.<anonymous> (/Users/aj/code/Infinitas/app-ecommerce-web/node_modules/next/dist/server/app-render/async-local-storage.js:27:72)

Code Example

// Here
//const { AsyncLocalStorage } = require("node:async_hooks");
//if (typeof globalThis.AsyncLocalStorage !== "function") {
//  globalThis.AsyncLocalStorage = AsyncLocalStorage;
//}

const express = require("express");
const { createHttpTerminator } = require("http-terminator");
const morgan = require("morgan");
const next = require("next");
const { handleTokenManagerProxy } = require("../utils/handleProxyRequests");
const handleRedirects = require("../utils/handleRedirects");
const backLogger = require("../utils/backLogger");
const port = Number.parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const nextApp = next({ dev, port });
const defaultHandler = nextApp.getRequestHandler();
const getConfig = require("next/config").default;

...

const init = async () => {
  await nextApp.prepare();
  const nextConfig = getConfig();

  const app = express();
  app.disable("x-powered-by");
  app.enable("trust proxy");
  app.enable("case sensitive routing");

  const server = app.listen(port, (err) => {
    if (err) {
      throw err;
    }

    backLogger.info(`ready on http://localhost:${port}`);
  });

  const { gracefulTerminationTimeout } = nextConfig.serverRuntimeConfig;
  const httpTerminator = createHttpTerminator({ server, gracefulTerminationTimeout });

  const gracefulShutdown = async (signal) => {
    backLogger.info(
      `⛔️ ${signal} received, gracefully shutting down (timeout ${gracefulTerminationTimeout}ms)`,
    );
    await httpTerminator.terminate();
    backLogger.info("Done! 👋");
    process.exit(0);
  };

  for (signal of ["SIGTERM"]) {
    process.on(signal, () => gracefulShutdown(signal));
  }
};

init().catch((e) => {
  backLogger.error(e);
  process.exit(-1);
});

---

Error: Invariant: AsyncLocalStorage accessed in runtime where it is not available
    at Object.<anonymous> (/Users/aj/code/Infinitas/app-ecommerce-web/node_modules/next/dist/server/app-render/async-local-storage.js:27:72)

---

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:06 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8103
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 20.19.5
  npm: 10.8.2
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 15.5.5 // An outdated version detected (latest is 16.0.6), upgrade is highly recommended!
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.5
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/austingayler/wormscan

To Reproduce

I'm moving from 14 -> 15 for my app. My server.js looks like this:

// Here
//const { AsyncLocalStorage } = require("node:async_hooks");
//if (typeof globalThis.AsyncLocalStorage !== "function") {
//  globalThis.AsyncLocalStorage = AsyncLocalStorage;
//}

const express = require("express");
const { createHttpTerminator } = require("http-terminator");
const morgan = require("morgan");
const next = require("next");
const { handleTokenManagerProxy } = require("../utils/handleProxyRequests");
const handleRedirects = require("../utils/handleRedirects");
const backLogger = require("../utils/backLogger");
const port = Number.parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== "production";
const nextApp = next({ dev, port });
const defaultHandler = nextApp.getRequestHandler();
const getConfig = require("next/config").default;

...

const init = async () => {
  await nextApp.prepare();
  const nextConfig = getConfig();

  const app = express();
  app.disable("x-powered-by");
  app.enable("trust proxy");
  app.enable("case sensitive routing");

  const server = app.listen(port, (err) => {
    if (err) {
      throw err;
    }

    backLogger.info(`ready on http://localhost:${port}`);
  });

  const { gracefulTerminationTimeout } = nextConfig.serverRuntimeConfig;
  const httpTerminator = createHttpTerminator({ server, gracefulTerminationTimeout });

  const gracefulShutdown = async (signal) => {
    backLogger.info(
      `⛔️ ${signal} received, gracefully shutting down (timeout ${gracefulTerminationTimeout}ms)`,
    );
    await httpTerminator.terminate();
    backLogger.info("Done! 👋");
    process.exit(0);
  };

  for (signal of ["SIGTERM"]) {
    process.on(signal, () => gracefulShutdown(signal));
  }
};

init().catch((e) => {
  backLogger.error(e);
  process.exit(-1);
});

Current vs. Expected behavior

During startup of 15, without the first 4 commented lines dealing with the AsyncLocalStorage it crashed with the error:

Error: Invariant: AsyncLocalStorage accessed in runtime where it is not available
    at Object.<anonymous> (/Users/aj/code/Infinitas/app-ecommerce-web/node_modules/next/dist/server/app-render/async-local-storage.js:27:72)

Enabling the 4 lines at the top fixes the error. I see it's the same fix as what's here: https://github.com/vercel/next.js/blob/canary/packages/next/src/server/node-environment-baseline.ts#L5-L9

Is this the correct way to fix this problem?

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.3.0: Thu Jan  2 20:24:06 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T8103
  Available memory (MB): 16384
  Available CPU cores: 8
Binaries:
  Node: 20.19.5
  npm: 10.8.2
  Yarn: 1.22.19
  pnpm: N/A
Relevant Packages:
  next: 15.5.5 // An outdated version detected (latest is 16.0.6), upgrade is highly recommended!
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.4.5

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

Webpack, Middleware, Module Resolution

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

next dev (local)

Additional context

No response

extent analysis

TL;DR

The issue can be fixed by enabling the commented lines at the top of the server.js file that deal with AsyncLocalStorage, which is a known fix for Next.js version 15.

Guidance

  • The error message indicates that AsyncLocalStorage is being accessed in a runtime where it is not available, which is a known issue in Next.js version 15.
  • Enabling the commented lines at the top of the server.js file, which set globalThis.AsyncLocalStorage to AsyncLocalStorage from node:async_hooks, fixes the error.
  • This fix is consistent with the solution provided in the Next.js repository.
  • Upgrading to the latest version of Next.js (16.0.6) may also resolve the issue, as the current version (15.5.5) is outdated.

Example

No code snippet is necessary, as the fix involves simply uncommenting existing lines in the server.js file.

Notes

The provided fix assumes that the issue is specific to Next.js version 15 and may not be applicable to other versions. Additionally, upgrading to the latest version of Next.js may introduce other changes or breaking updates.

Recommendation

Apply the workaround by enabling the commented lines at the top of the server.js file, as this is a known fix for the issue and does not require upgrading to a new version of Next.js.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING