{"id":19184,"library":"babel-plugin-transform-commonjs","title":"babel-plugin-transform-commonjs","description":"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.","status":"active","version":"1.1.6","language":"javascript","source_language":"en","source_url":"https://github.com/tbranyen/babel-plugin-transform-commonjs","tags":["javascript","babel","transform","plugin","commonjs","modules","esm"],"install":[{"cmd":"npm install babel-plugin-transform-commonjs","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-transform-commonjs","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-transform-commonjs","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as a peer dependency; plugin runs within Babel's transform pipeline.","package":"@babel/core","optional":false}],"imports":[{"note":"The plugin is an ESM default export. In CommonJS context, use require and access .default.","wrong":"const plugin = require('babel-plugin-transform-commonjs'); const { default: plugin } = require('babel-plugin-transform-commonjs')","symbol":"default export","correct":"import plugin from 'babel-plugin-transform-commonjs'"},{"note":"String shorthand is preferred in Babel config; if passing options, use array form: [['transform-commonjs', { ... }]].","wrong":"{ plugins: [require('babel-plugin-transform-commonjs')] }","symbol":"plugin as object","correct":"{ plugins: ['transform-commonjs'] }"},{"note":"The option is 'exportsOnly', not 'onlyExports' (documented as 'onlyExports' but the code uses 'exportsOnly' — check version).","wrong":"{ plugins: [['transform-commonjs', { onlyExports: true }]] }","symbol":"options","correct":"{ plugins: [['transform-commonjs', { synchronousImport: true, exportsOnly: true }]] }"}],"quickstart":{"code":"npm install --save-dev babel-plugin-transform-commonjs @babel/core\n\necho '{\n  \"plugins\": [\"transform-commonjs\"]\n}' > .babelrc\n\necho 'var { readFileSync } = require(\"path\");\nexports.readFileSync = readFileSync;' > input.js\n\nnpx babel input.js","lang":"javascript","description":"Installs the plugin, configures Babel, and transforms a CJS file with named exports."},"warnings":[{"fix":"Use 'synchronousImport: true' to convert non-static require to dynamic import (may produce invalid code outside bundler).","message":"Non-static require calls (e.g., require(condition ? 'a' : 'b')) raise an exception by default.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Refactor exports to use valid identifiers or access via default export.","message":"Invalid named exports (e.g., exports[\"I'mateapot\"]) are not available as named exports; they only appear on default export.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use 'exportsOnly' instead of 'onlyExports'.","message":"The option 'onlyExports' is documented but the code expects 'exportsOnly'. Check your Babel config.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Install @babel/core@^7 and ensure @babel/core is in devDependencies.","cause":"Using an outdated version of @babel/core (<7) or incompatible Babel version.","error":"Error: [BABEL] unknown: You gave us a visitor for the node type \"Expression\" but it's not a valid type"},{"fix":"Ensure bundler (e.g., webpack) polyfills Node core modules, or use a bundler that supports them natively.","cause":"The plugin does not transform require calls for Node built-ins; bundler may fail if it expects a full ESM environment.","error":"Module not found: Can't resolve 'path'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}