canirequire

canirequire › CommonJS › invariant

Is invariant ESM-only? Can you require() it?

invariant

Last verified Sep 9, 2026 against invariant@2.2.4. Markdown · JSON

CommonJS CommonJS — require() works on every Node.js version we test

invariant ships CommonJS only. require("invariant") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. import "invariant" also works because Node.js and Bun can import CommonJS from ES modules, but only the default export is guaranteed; named imports depend on Node's static analysis of the module.

What to do

  1. require("invariant") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun.
  2. From ES modules use the default import: import invariant from "invariant"; then destructure. Named imports only work when Node.js can statically detect the exports.
  3. TypeScript: npm install -D @types/invariant
Latest
2.2.4 (Mar 13, 2018)
Module format
CommonJS
Last CommonJS version
2.2.4
TypeScript types
@types/invariant
Node engines
not declared
Weekly downloads
29.9M
License
MIT

Does invariant@2.2.4 actually load? Runtime test results

Installed and executed on Sep 9, 2026 with require("invariant") from a CommonJS file and import "invariant" from an ES module.

Runtimerequire("invariant")import "invariant"
Node.js 18✓ works✓ works
Node.js 20✓ works✓ works
Node.js 22✓ works✓ works
Node.js 24✓ works✓ works
Node.js 26✓ works✓ works
Bun 1.4.0✓ works✓ works

How to import invariant

ES modules (import)

import invariant from "invariant";

Named imports from a CommonJS package only work when Node.js can statically detect them. If a named import fails, use the default import and destructure.

CommonJS (require)

const invariant = require("invariant");

Module format by version

Derived from the type, main and exports fields of every stable release on npm, corrected where a runtime test of that version contradicted them (13 versions; the newest 13 shown).

VersionsFormatFirst published
1.0.0 – 2.2.4CommonJSSep 24, 2014

Frequently asked

Is invariant ESM-only?

invariant@2.2.4 is a CommonJS package. invariant ships CommonJS only. require("invariant") works on every Node.js version we test (18, 20, 22, 24 and 26) and in Bun. import "invariant" also works because Node.js and Bun can import CommonJS from ES modules, but only the default export is guaranteed; named imports depend on Node's static analysis of the module.

Can I import invariant with ES module syntax?

Yes. import invariant from "invariant" works in Node.js and Bun because they support importing CommonJS modules. Named imports work only when Node.js can statically detect the exports.

Does invariant work in Bun?

Yes. import "invariant" loaded successfully in Bun 1.4.0 on Sep 9, 2026.

Does invariant ship TypeScript types?

Not bundled. Install the community types with "npm install -D @types/invariant".

Links

Other commonjs packages

Similar popularity