DisposableStack is not defined: why it happens and how to fix it

IntermediateJavaScript interview questionBy , updated 3 December 2026

Short answer

ReferenceError: DisposableStack is not defined means some code calls new DisposableStack() in a runtime that does not have that global yet. DisposableStack is part of Explicit Resource Management, together with using and Symbol.dispose. As of December 2026, Chrome and Edge 134+, Firefox 141+, Node.js 24+, Deno 2.2.10+ and Bun 1.3+ ship it, and Safari adds it in 27.3, which is still in beta. Upgrade the runtime or load a polyfill such as core-js.

What the error means

A ReferenceError is thrown when code reads a variable that does not exist in any scope. Here the missing variable is the global constructor DisposableStack. Nothing is wrong with your logic. The engine running it is simply older than the feature.

Explicit Resource Management is a TC39 proposal that has reached Stage 4, so it is part of the language. It adds several pieces at once:

  • using and await using declarations, which call [Symbol.dispose]() or [Symbol.asyncDispose]() when the variable's scope ends.
  • DisposableStack and AsyncDisposableStack, objects that collect cleanup callbacks and run them in reverse order.
  • SuppressedError, which combines a cleanup error with the error that was already being thrown.
  • The well-known symbols Symbol.dispose and Symbol.asyncDispose.
function openPanel() {
  using stack = new DisposableStack();
  const socket = stack.use(openSocket()); // disposed last
  stack.defer(() => clearInterval(timer)); // runs first
  // ...
} // the stack disposes everything here, in reverse order

On an engine without the feature, the first line of that function fails. If the engine also lacks using, the file fails earlier, with a SyntaxError at parse time.

Which runtimes ship DisposableStack and using

As of December 2026, from MDN's compatibility data:

Runtime DisposableStack using
Chrome and Edge 134 134
Firefox 141 141
Safari (macOS and iOS) 27.3, in beta 27.3, in beta
Node.js 24.0.0 24.0.0
Deno 2.2.10 1.37
Bun 1.3.0 1.0.23

Two gaps cause most reports. Node.js 22 and older do not have it, and many servers, CI images and CLI tools still run Node 22. Safari's stable releases do not have it yet, so a page that works in Chrome during development can fail for Safari users in production.

Why it happens

  • An old Node.js version. A package or command-line tool was built for Node 24 or newer and you run it on an older version. Run node --version first. This is the most common cause of the error at startup.
  • Old or not-yet-updated browsers. Your bundle ships new DisposableStack() to every visitor, but Safari before 27.3 and older Chrome or Firefox versions lack it.
  • TypeScript types say it exists. With "lib": ["esnext"] or "esnext.disposable", the type checker knows DisposableStack, so the code compiles. TypeScript only checks types. It does not add the class at runtime.
  • You transpiled using and thought that was enough. Babel and TypeScript can rewrite using into try/finally code with helpers. That fixes the syntax, but a direct new DisposableStack() call still needs the global.
  • Tests run in an older runtime. A test runner on Node 22 fails even when the app itself runs in a modern browser.

Fix 1: upgrade the runtime

If you control where the code runs, such as a server, CI or your own machine, move to Node.js 24 or newer. Record the requirement so the next person does not hit the same error:

{
  "engines": { "node": ">=24" }
}

Also update the Node version in your CI configuration, Dockerfile and .nvmrc. For a CLI tool that fails with this error, upgrading Node is usually the whole fix.

Fix 2: load a polyfill

For browsers you do not control, load a polyfill before any code that uses the feature. core-js implements DisposableStack, AsyncDisposableStack, SuppressedError and both symbols:

// polyfills.js
import "core-js/actual/disposable-stack";
import "core-js/actual/async-disposable-stack";
import "core-js/actual/suppressed-error";
import "core-js/actual/symbol/dispose";
import "core-js/actual/symbol/async-dispose";
// main.js: the polyfill module must be the first import
import "./polyfills.js";
import "./app.js";

Imports run in order, so app.js sees the globals. A library should not patch globals for its users. It can import the constructor from core-js-pure instead, which leaves the global scope alone.

To see what such a polyfill has to do, including reverse-order disposal and SuppressedError chaining, try Polyfill DisposableStack: use, adopt, defer, move, reverse-order disposal and SuppressedError.

Fix 3: transpile using declarations

If the error comes with using syntax that older engines cannot parse, compile it away. The rule is simple: syntax needs a transpiler, and missing globals need a polyfill. The answer on polyfills vs transpilers explains the split.

  • TypeScript 5.2 and newer rewrites using when target is es2022 or lower. Its release notes say the emitted code still needs Symbol.dispose and Symbol.asyncDispose, and that a small shim is enough if you only use using:
Symbol.dispose ??= Symbol("Symbol.dispose");
Symbol.asyncDispose ??= Symbol("Symbol.asyncDispose");
  • Babel has @babel/plugin-transform-explicit-resource-management, which is also included in @babel/preset-env. It turns using into a try/catch/finally block with a helper.

Neither tool adds DisposableStack. If your code constructs one, you need Fix 1 or Fix 2 as well.

Common follow-up questions

Why does TypeScript compile the code if the class does not exist?

Because lib describes the environment you promise to run in. Adding esnext.disposable tells the compiler the class will be there. It does not check that promise against real browsers or Node versions.

Is a SyntaxError on using the same problem?

It is the same feature, one step earlier. An engine without using cannot parse the file, so it fails before any code runs. Transpile the syntax, and polyfill the globals if you also call them.

Do I need AsyncDisposableStack too?

Only if you use it or await using. They shipped in the same releases, so a runtime missing one is missing both. Polyfilling both at once is the safe choice.

Can I avoid the feature entirely?

Yes. A try/finally block that runs your cleanup functions in reverse order covers most cases on any runtime. You lose the built-in SuppressedError handling, so catch cleanup errors yourself.

Mistakes to avoid

  • Loading the polyfill after the module that calls new DisposableStack()

    Import order matters.

  • Treating the TypeScript lib setting as runtime support

  • Assuming that transpiling using also provides DisposableStack

  • Testing only in Chrome and shipping to Safari users before 27.3 is stable

  • Patching globals from inside a published library instead of using core-js-pure

Practise it in code

Interviewers rarely stop at the definition. These questions open in an editor with tests.

  1. Polyfill DisposableStack: use, adopt, defer, move, reverse-order disposal and SuppressedErrorMedium35 min
  2. Implement structured concurrency with withTaskGroup: shared cancellation, first error, cleanup and parent abortHard50 min
  3. Build a CancellablePromise: cancel(), onCancel cleanup and cancellation through chainsMedium40 min
  4. Fix a DOM node registry that leaks: callback refs with cleanup functionsHard40 min
  5. Implement a memory-efficient event bus with lifecycle cleanup and leak detectionHard60 min