Registry / web-framework / babel-plugin-transform-commonjs

babel-plugin-transform-commonjs

JSON →
library1.1.6jsnpmunverified

A Babel 7 plugin that transforms CommonJS modules (require, module.exports, exports) into ES modules (import/export). Version 1.1.6 is the latest stable release. It aims to produce spec-compliant ESM code and throws on unsupported patterns like non-static require or require.extensions. It offers escape hatches (synchronousImport, exportsOnly) for controlled conversion. Unlike @babel/plugin-transform-modules-commonjs (which targets the opposite direction), this plugin converts CJS to ESM and is mainly used for building ESM-compatible bundles or migration.

npm install babel-plugin-transform-commonjs
INSTALL
IMPORT
SIG · BABEL-PLUGIN-TRANS
B
babel-plugin-transform-commonjs
web-frameworkjavascriptv1.1.6
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

default export
import plugin from 'babel-plugin-transform-commonjs'
const plugin = require('babel-plugin-transform-commonjs'); const { default: plugin } = require('babel-plugin-transform-commonjs')
The plugin is an ESM default export. In CommonJS context, use require and access .default.
plugin as object
{ plugins: ['transform-commonjs'] }
{ plugins: [require('babel-plugin-transform-commonjs')] }
String shorthand is preferred in Babel config; if passing options, use array form: [['transform-commonjs', { ... }]].
options
{ plugins: [['transform-commonjs', { synchronousImport: true, exportsOnly: true }]] }
{ plugins: [['transform-commonjs', { onlyExports: true }]] }
The option is 'exportsOnly', not 'onlyExports' (documented as 'onlyExports' but the code uses 'exportsOnly' — check version).

Installs the plugin, configures Babel, and transforms a CJS file with named exports.

npm install --save-dev babel-plugin-transform-commonjs @babel/core echo '{ "plugins": ["transform-commonjs"] }' > .babelrc echo 'var { readFileSync } = require("path"); exports.readFileSync = readFileSync;' > input.js npx babel input.js
Debug
Known issues
breakingNon-static require calls (e.g., require(condition ? 'a' : 'b')) raise an exception by default.
fix
Use 'synchronousImport: true' to convert non-static require to dynamic import (may produce invalid code outside bundler).
affects: >=1.0.0
gotchaInvalid named exports (e.g., exports["I'mateapot"]) are not available as named exports; they only appear on default export.
fix
Refactor exports to use valid identifiers or access via default export.
affects: >=1.0.0
deprecatedThe option 'onlyExports' is documented but the code expects 'exportsOnly'. Check your Babel config.
fix
Use 'exportsOnly' instead of 'onlyExports'.
affects: >=1.0.0
Errors
Common errors & fixes
Error: [BABEL] unknown: You gave us a visitor for the node type "Expression" but it's not a valid type
Using an outdated version of @babel/core (<7) or incompatible Babel version.
fix
Install @babel/core@^7 and ensure @babel/core is in devDependencies.
Module not found: Can't resolve 'path'
The plugin does not transform require calls for Node built-ins; bundler may fail if it expects a full ESM environment.
fix
Ensure bundler (e.g., webpack) polyfills Node core modules, or use a bundler that supports them natively.
Upgrade
Version history
1.1.6latest on npm
Audit
Dependencies
@babel/corerequiredRequired as a peer dependency; plugin runs within Babel's transform pipeline.
Agent activity
10 hits · last 30 days
node
8
OpenAI (training)
2
Resources
babel-plugin-transform-commonjs — npm install babel-plugin-transform-commonjs · libregistry