{"id":19113,"library":"babel-plugin-node-cjs-interop","title":"Babel Plugin Node CJS Interop","description":"A Babel plugin (v0.1.9) that fixes the default import interoperability issue when importing simulated ESM (CommonJS modules with __esModule flag) from native ES modules in Node.js. This plugin automatically wraps export values to ensure default imports work correctly. Compared to alternatives, it automates the wrapping process. Maintained by qnighy, with releases as needed. Also available as swc-plugin-node-cjs-interop for SWC users.","status":"active","version":"0.1.9","language":"javascript","source_language":"en","source_url":"https://github.com/qnighy/node-cjs-interop","tags":["javascript","babel","babel-plugin","commonjs","node","esm","mjs","cjs","default import","typescript"],"install":[{"cmd":"npm install babel-plugin-node-cjs-interop","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-node-cjs-interop","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-node-cjs-interop","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency required for Babel plugin functionality","package":"@babel/core","optional":false}],"imports":[{"note":"Configure as a Babel plugin, not imported directly. For CommonJS configs, use module.exports.","wrong":"// CommonJS require can't be used directly in .babelrc.js","symbol":"babel-plugin-node-cjs-interop","correct":"// In .babelrc.js or babel.config.js\nplugins: [\n  ['babel-plugin-node-cjs-interop', { packages: ['styled-components'] }]\n]"}],"quickstart":{"code":"// Install: npm install -D babel-plugin-node-cjs-interop\n\n// .babelrc.js\nmodule.exports = {\n  presets: [\n    ['@babel/preset-env', { modules: false }]\n  ],\n  plugins: [\n    ['babel-plugin-node-cjs-interop', {\n      packages: ['styled-components', 'some-package']\n    }]\n  ]\n};\n\n// Then build with Babel. The plugin will add compatibility wrappers\n// to ensure default imports work from native ESM.","lang":"javascript","description":"Shows how to install and configure the Babel plugin to fix default import interoperability with native ES modules."},"warnings":[{"fix":"Manually wrap re-exported modules or avoid re-exports from simulated ESM packages.","message":"The plugin does NOT detect re-exports (export ... from). Modules using re-exports may still cause default import failures.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Be aware that importing mutable exports from simulated ESM may behave differently. Test thoroughly.","message":"Named imports from simulated ESM modules may not reflect live bindings correctly when imported from native ESM. The plugin restores live update semantics but can cause behavioral differences in rare cases.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure all named exports exist in the imported package. Use TypeScript or check package exports.","message":"Importing a non-existent named export from a simulated ESM module with this plugin will return undefined instead of throwing an error like native ESM does.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Limit the packages list to only those experiencing issues. Consider using node-cjs-interop-finder to detect problematic packages.","message":"The plugin may negatively affect tree-shaking because wrapper functions obscure unused import analysis.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Manually verify affected packages and test imports.","message":"False positives can occur if a package uses the __esModule flag for purposes other than indicating Babel-transpiled ES modules.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Add the problematic package to babel-plugin-node-cjs-interop's packages list.\nExample: packages: ['styled-components']","cause":"Default import from a simulated ESM (CommonJS with __esModule) to native ESM fails because the module exports a default property but Node.js ESM expects direct function export.","error":"TypeError: greet is not a function\n    at ./b.mjs:3:1"},{"fix":"Use babel-plugin-node-cjs-interop with the package name listed in the configuration.","cause":"Node.js ESM cannot find the default export from a CommonJS module that has __esModule flag.","error":"SyntaxError: The requested module '...' does not provide an export named 'default'"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}