canirequire

canirequire › CommonJS › npm

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

a package manager for JavaScript

Last verified Sep 9, 2026 against npm@12.0.2. Markdown · JSON

CommonJS CommonJS on paper — require() failed in our runtime test

npm declares a CommonJS entry only, but require("npm") failed on every Node.js version in our probe: The programmatic API was removed in npm v8.0.0. import "npm" resolves to the same file, so it fails the same way. That usually means a missing peer dependency, a browser-only global, or a package that is not meant to be loaded directly; see the runtime table below.

What to do

  1. require("npm") failed in our probe on every Node.js version (The programmatic API was removed in npm v8.0.0); check the package README for peer dependencies or setup before relying on it.
  2. From ES modules use the default import: import npm from "npm"; then destructure. Named imports only work when Node.js can statically detect the exports.
  3. TypeScript: npm install -D @types/npm
Latest
12.0.2 (Jul 29, 2026)
Module format
CommonJS
Last CommonJS version
12.0.2
TypeScript types
@types/npm
Node engines
^22.22.2 || ^24.15.0 || >=26.0.0
Weekly downloads
13.9M
License
Artistic-2.0

Does npm@12.0.2 actually load? Runtime test results

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

Runtimerequire("npm")import "npm"
Node.js 18✗ failsError✗ failsError
Node.js 20✗ failsError✗ failsError
Node.js 22✗ failsError✗ failsError
Node.js 24✗ failsError✗ failsError
Node.js 26✗ failsError✗ failsError
Bun 1.4.0✗ failsError✗ failsError

A failing require() on a CommonJS package usually means the package needs a browser/DOM global, a peer dependency, or a native binary that is not available in a bare Node.js process.

How to import npm

ES modules (import)

import npm from "npm";

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)

// require("npm") failed in our probe on every Node.js version we test (The programmatic API was removed in npm v8.0.0); see the runtime table.
const npm = require("npm");

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 (527 versions; the newest 60 shown).

VersionsFormatFirst published
10.2.3 – 12.0.2CommonJSNov 2, 2023

Frequently asked

Is npm ESM-only?

npm@12.0.2 declares a CommonJS entry, but require() fails at runtime. npm declares a CommonJS entry only, but require("npm") failed on every Node.js version in our probe: The programmatic API was removed in npm v8.0.0. import "npm" resolves to the same file, so it fails the same way. That usually means a missing peer dependency, a browser-only global, or a package that is not meant to be loaded directly; see the runtime table below.

Can I require npm?

Not in practice. npm declares a CommonJS entry, but require("npm") failed on every Node.js version in our probe: The programmatic API was removed in npm v8.0.0. Check the package README for peer dependencies or setup before relying on it.

Can I import npm with ES module syntax?

Yes. import npm from "npm" 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 npm work in Bun?

Importing npm failed in Bun 1.4.0 on Sep 9, 2026: error: The programmatic API was removed in npm v8.0.0

Does npm ship TypeScript types?

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

Links

Other commonjs packages

Similar popularity