{"id":19222,"library":"babel-plugin-transform-require","title":"babel-plugin-transform-require","description":"A Babel plugin that transforms CommonJS require() calls into ES6 import statements. Version 1.1.0 is the latest stable release. It converts patterns like var a = require('a') to import a from 'a'. This plugin is useful for incremental migration of codebases from CommonJS to ES modules. It operates as a Babel plugin and requires babel-core (or @babel/core) to function. Unlike more comprehensive tools like @babel/plugin-transform-modules-commonjs, this plugin focuses solely on require-to-import conversion without handling exports. The package has a slow release cadence and is minimal in scope.","status":"active","version":"1.1.0","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","es5","es6","es2015","es7","es2016","transpiler","transplie","require"],"install":[{"cmd":"npm install babel-plugin-transform-require","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-transform-require","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-transform-require","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The package exports a default function. Using CommonJS require works in Node but may break in ESM-only environments.","wrong":"const transformRequire = require('babel-plugin-transform-require')","symbol":"default (transformRequire)","correct":"import transformRequire from 'babel-plugin-transform-require'"},{"note":"There is no named export; using destructuring will result in undefined.","wrong":"const { transformRequire } = require('babel-plugin-transform-require')","symbol":"Node require (CJS)","correct":"const transformRequire = require('babel-plugin-transform-require')"},{"note":"Babel resolves plugins with the 'babel-plugin-' prefix automatically, but the full name also works.","wrong":"{\"plugins\": [\"babel-plugin-transform-require\"]}","symbol":"In .babelrc plugins array","correct":"{\"plugins\": [\"transform-require\"]}"}],"quickstart":{"code":"// Install: npm install --save-dev babel-core babel-plugin-transform-require\nimport {transform} from 'babel-core';\nimport transformRequire from 'babel-plugin-transform-require';\n\nconst codeES5 = 'var a = require(\"a\")';\nconst codeES6 = transform(codeES5, {\n  plugins: [transformRequire]\n}).code;\n\nconsole.log(codeES6); // import a from \"a\";","lang":"typescript","description":"Shows how to transform a require call to an import statement using the plugin programmatically with babel-core."},"warnings":[{"fix":"Use additional plugins like @babel/plugin-transform-modules-commonjs for a full CJS-to-ESM conversion.","message":"The plugin only transforms require() calls. It does not handle module.exports or exports, so those remain as-is.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Manually refactor dynamic requires or use a runtime helper for dynamic imports.","message":"The plugin does not support dynamic require() patterns (e.g., var x = require(someVar)). Such calls are left unchanged.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Manually adjust code to use let or const and ensure no reassignment of imported bindings.","message":"If the require() result is assigned to a variable that is reassigned later, the import statement will not be semantically equivalent. E.g., var a = require('a'); a = 5; becomes import a from 'a'; a = 5; which is invalid.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Run npm install --save-dev babel-core (or @babel/core for Babel 7+).","message":"The plugin relies on babel-core (or @babel/core) but does not declare it as a peer dependency. Users must install it separately.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use a more modern plugin or migrate to @babel/plugin-transform-modules-commonjs.","message":"The package is based on the deprecated babel-core API (Babel 6). It may not work with Babel 7+ without adjustments.","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":"Run npm install --save-dev babel-core (or @babel/core) to install babel-core as a development dependency.","cause":"babel-core is not installed or is missing from node_modules.","error":"Error: Cannot find module 'babel-core'"},{"fix":"Use import transformRequire from 'babel-plugin-transform-require' without curly braces.","cause":"The import is destructured incorrectly (e.g., { transformRequire }) but the package exports a default function.","error":"TypeError: transformRequire is not a function"},{"fix":"Use the plugin to transform the require calls, or ensure that Node.js is configured with --experimental-require-module flag if applicable.","cause":"Using require() in an ES module environment without proper bundler/transpiler setup.","error":"ReferenceError: require is not defined"},{"fix":"Use a bundler like Webpack or a Node.js version that supports ES modules with --experimental-modules flag.","cause":"The transformed code contains import statements but the environment does not support ES modules.","error":"SyntaxError: Unexpected token import"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}