Registry / serialization / esmod
library1.0.0jsnpmunverified

esmod is a fast in-place transpiler for converting ES6 import/export statements to CommonJS require/exports. Version 1.0.0 is currently stable, with a focus on speed by leveraging AST-based transformation rather than regex-only approaches. It distinguishes itself from tools like esbuild or Babel by being designed for lightweight, single-file transformations without module resolution or bundling — ideal for build tool authors and custom pipelines.

npm install esmod
INSTALL
IMPORT
SIG · ESMOD
E
esmod
serializationjavascriptv1.0.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.

transform
import { transform } from 'esmod'
const transform = require('esmod').transform
ESM-only package; CommonJS require() does not work natively unless in a Node ESM context.
transformSync
import { transformSync } from 'esmod'
import transformSync from 'esmod'
transformSync is a named export, not a default export.
default transform
import transform from 'esmod'
const { transform } = require('esmod')
The default export is a shorthand for the transform function.

Converts an ES module file (.mjs) to CommonJS (.cjs) using esmod's transform function with explicit source and target formats.

import { transform } from 'esmod'; import { readFileSync, writeFileSync } from 'fs'; const code = readFileSync('input.mjs', 'utf8'); const result = transform(code, { from: 'esm', to: 'cjs' }); writeFileSync('output.cjs', result.code); console.log('Transformed successfully!');
esmod --version
Debug
Known issues
gotchatransform() throws synchronous errors; do not use try/catch with async error patterns.
fix
Wrap in try/catch (not .catch()) since it's not a Promise.
affects: >=1.0.0
gotchaInput code must contain only valid ES module syntax; mixing require() and import will cause parse errors.
fix
Ensure code is pure ES module syntax before passing to transform.
affects: >=1.0.0
deprecatedoptions.from and options.to are optional; default is ESM-to-ESM (no-op), which may be unexpected.
fix
Always specify both targets to avoid accidental no-op.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'code')
Calling transform() without waiting for a Promise, but it's actually synchronous and returns an object directly.
fix
Access result.code immediately after calling transform() without await.
SyntaxError: Unexpected token 'export'
Input contains export statements but options.from is not set to 'esm', so transform treats input as CommonJS.
fix
Set { from: 'esm' } in options.
Error: No transform applied because source and target are the same.
Both from and to options are set to 'esm', resulting in no transformation.
fix
Set 'to' to 'cjs' to convert to CommonJS.
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
esmod — npm install esmod · libregistry