Registry / serialization / cjs-module-lexer

cjs-module-lexer

JSON →
library2.2.0jsnpmunverified

A fast CommonJS module syntax lexer that detects named exports and reexports from CJS modules, used in Node.js core for ESM interop. Current stable version: 2.2.0. Maintained by Node.js team; release cadence is irregular but active for parser bugs and performance improvements. Key differentiators: extremely fast (~90ms/MB cold, ~15ms/MB warm), frozen detection patterns to ensure backwards compatibility across Node.js versions, and support for transpiler variations (e.g., TypeScript, Babel). Provides both Node.js (CJS) and Wasm (ESM) builds, with TypeScript declarations included.

npm install cjs-module-lexer
INSTALL
IMPORT
SIG · CJS-MODULE-LEXER
C
cjs-module-lexer
serializationjavascriptv2.2.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

parse
import { parse, init } from 'cjs-module-lexer'
const parse = require('cjs-module-lexer').parse
For ESM, init() must be awaited before first parse() call. For CJS, init is automatically handled.
init
import { init } from 'cjs-module-lexer'
const { init } = require('cjs-module-lexer')
init() is only needed for Wasm build (ESM). In CJS, it's a no-op promise.
parse
const { parse } = require('cjs-module-lexer'); parse(source)
const cjsModuleLexer = require('cjs-module-lexer'); cjsModuleLexer.parse(source)
CJS import: destructure parse from the module. No need to call init().

Demonstrates ESM usage with Wasm: import parse and init, await init(), parse a CJS source, and obtain exports/reexports arrays.

import { parse, init } from 'cjs-module-lexer'; const source = ` module.exports.a = 'a'; if (maybe) module.exports = require('./dep1.js'); module.exports = { b, c: d }; `; await init(); const { exports, reexports } = parse(source); console.log(exports); // ['a', 'b', 'c', '__esModule'] console.log(reexports); // ['./dep1.js']
Debug
Known issues
breakingIn v2.0.0, the Wasm build was introduced and the ESM API changed: init() must be called before parse(). Previously, init() was not required and Wasm was not used.
fix
Add await init() before calling parse() in ESM environments. CJS users are unaffected.
affects: >=2.0.0
deprecatedThe detection patterns are frozen; new export patterns will not be added. This ensures backwards compatibility but means some modern CJS patterns may not be detected.
fix
Do not rely on detection of non-standard patterns. Ensure your code uses the documented export forms (e.g., exports.X = ..., module.exports = { ... }, Object.defineProperty(exports, ...)).
affects: >=1.0.0
gotchaThe lexer does not handle all JavaScript edge cases (e.g., dynamic require, conditional exports inside functions). It uses a token grammar that may miss exports with unusual whitespace or comments.
fix
Test your modules with the lexer to ensure expected exports are detected. For complex cases, consider alternative tools.
affects: >=1.0.0
gotchaThe `__esModule` export is always included in the exports array if Object.defineProperty(module.exports, '__esModule', ...) is present. This may be unexpected for some users.
fix
Filter out '__esModule' from exports if not needed: exports.filter(e => e !== '__esModule')
affects: >=1.0.0
breakingVersion 1.4.2 included a change that moved to a common wasm-builder, but this was later refined. Users upgrading from 1.x should test exports detection thoroughly.
fix
Upgrade to 2.2.0 or later, and verify export detection.
affects: 1.4.2
Errors
Common errors & fixes
Error: Must call init() before parse()
Using ESM import with Wasm build but not calling await init() first.
fix
Add await init(); before any parse() call in ESM context.
TypeError: parse is not a function
Importing cjs-module-lexer incorrectly in CJS, e.g., using default import or wrong destructure.
fix
Use const { parse } = require('cjs-module-lexer'); in CommonJS.
ReferenceError: exports is not defined
Trying to access exports in a context where the CJS wrapper (function(exports, module, require) { ... }) is not provided.
fix
Ensure source code is wrapped in a CommonJS module wrapper when lexing; the lexer expects valid CJS syntax.
Warning: cjs-module-lexer detected a reexport but the path './foo' is not quoted?
Using module.exports = require('./foo') without quotes around the path? Actually, the lexer expects string literals.
fix
Use require('./foo') with a string literal; ensure no dynamic expressions.
Upgrade
Version history
2.2.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
6
OpenAI (training)
1
Resources
cjs-module-lexer — npm install cjs-module-lexer · libregistry