{"id":12862,"library":"babel-plugin-dynamic-import-node","title":"Babel Plugin for Dynamic `import()` in Node.js","description":"This Babel plugin, `babel-plugin-dynamic-import-node`, transpiles ECMAScript `import()` expressions into deferred CommonJS `require()` calls specifically for Node.js environments. Its primary purpose is to enable the use of modern dynamic `import()` syntax in projects targeting Node.js versions that do not natively support ESM dynamic imports, or in mixed CommonJS/ESM contexts where asynchronous loading of CommonJS modules is required. The current stable version is 2.3.3, with its last significant updates occurring around 2019. While still functional for legacy projects, its necessity has largely diminished with the widespread adoption of native ESM support in modern Node.js versions and the capabilities of `@babel/preset-env` to handle dynamic imports for various targets. Key differentiators include its focus solely on `import()` to `require()` transformation, offering specific control over interoperability via the `noInterop` option.","status":"abandoned","version":"2.3.3","language":"javascript","source_language":"en","source_url":"https://github.com/airbnb/babel-plugin-dynamic-import-node","tags":["javascript","babel","plugin","dynamic","import","node"],"install":[{"cmd":"npm install babel-plugin-dynamic-import-node","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-dynamic-import-node","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-dynamic-import-node","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required as the core Babel transpiler engine. This plugin integrates with Babel's parser (Babylon) and transform pipeline.","package":"@babel/core","optional":false}],"imports":[{"note":"This plugin is loaded via Babel's configuration file (`.babelrc`, `babel.config.js`, etc.), not imported into application code.","wrong":"import dynamicImportNode from 'babel-plugin-dynamic-import-node';","symbol":"Config via .babelrc","correct":"{\n  \"plugins\": [\"dynamic-import-node\"]\n}"},{"note":"Plugin options must be provided as a second element in an array when specifying the plugin name in Babel configuration.","wrong":"{\n  \"plugins\": [\n    \"dynamic-import-node\", { \"noInterop\": true }\n  ]\n}","symbol":"Config with options","correct":"{\n  \"plugins\": [\n    [\"dynamic-import-node\", { \"noInterop\": true }]\n  ]\n}"},{"note":"When using Babel's Node.js API, the plugin itself is typically required using CommonJS syntax.","wrong":"import dynamicImportNode from 'babel-plugin-dynamic-import-node';","symbol":"Node API usage","correct":"const dynamicImportNode = require('babel-plugin-dynamic-import-node');"}],"quickstart":{"code":"const babel = require('@babel/core');\nconst fs = require('fs');\nconst path = require('path');\n\nconst inputCode = `\nasync function loadModules() {\n  const [mod1, mod2] = await Promise.all([\n    import('./lib/module1.js'),\n    import('./lib/module2.js')\n  ]);\n  console.log('Module 1:', mod1.default);\n  console.log('Module 2:', mod2.message);\n}\n\nloadModules();\n`;\n\n// Create dummy modules for demonstration\nfs.mkdirSync(path.join(__dirname, 'lib'), { recursive: true });\nfs.writeFileSync(path.join(__dirname, 'lib', 'module1.js'), 'module.exports = { default: \"Hello from Module 1!\" };');\nfs.writeFileSync(path.join(__dirname, 'lib', 'module2.js'), 'exports.message = \"Greetings from Module 2!\";');\n\ntry {\n  const result = babel.transformSync(inputCode, {\n    plugins: ['babel-plugin-dynamic-import-node'],\n    filename: 'test.js' // Needed for relative imports\n  });\n\n  console.log('Transpiled code:\\n', result.code);\n\n  // To run the transpiled code in Node.js, you'd typically save it to a file:\n  // fs.writeFileSync('output.js', result.code);\n  // require('./output.js');\n} catch (error) {\n  console.error('Babel transformation failed:', error);\n}\n","lang":"javascript","description":"Demonstrates how to use `babel-plugin-dynamic-import-node` via the Node.js API to transpile dynamic `import()` calls into `require()` for execution in a Node.js environment."},"warnings":[{"fix":"Ensure you are using a compatible version of Babel (e.g., Babel 7 with `@babel/core` and `@babel/parser`) or at least `babel-core@^6.12.0` if on Babel 6.","message":"This plugin requires a specific version of Babel's parser, Babylon >= v6.12.0, to correctly parse dynamic imports. Using an older Babel core or parser may result in `SyntaxError`s when encountering `import()` expressions.","severity":"gotcha","affected_versions":"<=2.3.3"},{"fix":"For new projects or upgrades, consider leveraging native Node.js ESM or configuring `@babel/preset-env` with appropriate targets. This plugin is primarily relevant for maintaining older projects or specific legacy setups.","message":"The primary use case for `babel-plugin-dynamic-import-node` has been largely superseded. Modern Node.js versions (12.x LTS and above, especially 14+) offer robust native ESM support for dynamic imports. Additionally, `@babel/preset-env` can handle `import()` transformations for various target environments, including Node.js, without requiring this specific plugin.","severity":"deprecated","affected_versions":">=1.0.0"},{"fix":"Use the `noInterop: true` option for the plugin: `[\"dynamic-import-node\", { \"noInterop\": true }]`. This will prevent the plugin from adding `.default` to the `require()` calls, allowing direct access to the CommonJS export.","message":"When transpiling CommonJS modules that do not export a `default` property (which is common for many CJS modules), the transpiled `require('module').default` call can result in `undefined` or runtime errors. This often occurs when importing a CJS module that uses `module.exports = ...` or `exports.foo = ...`.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure your Babel configuration includes `babel-plugin-dynamic-import-node` and that your `@babel/core` (or `babel-core`) version is recent enough (e.g., Babel 7+). This plugin handles the necessary syntax parsing automatically.","cause":"This error occurs when the Babel parser (Babylon) used by your setup is too old to understand the `import()` syntax, or if `babel-plugin-syntax-dynamic-import` (which this plugin implicitly depends on) is missing or not configured.","error":"SyntaxError: 'import' and 'export' may only appear at the top level"},{"fix":"Apply the `noInterop: true` option to the plugin configuration: `[\"dynamic-import-node\", { \"noInterop\": true }]`. This directs the plugin to output `require()` calls without appending `.default`.","cause":"This runtime error typically indicates that you are dynamically importing a CommonJS module that does not have a `default` export, but the plugin transpiled it assuming one. This is due to Babel's default interop behavior for `import` statements.","error":"TypeError: (0, _module.default) is not a function"},{"fix":"Ensure the transpiled output is executed within a Node.js environment. If targeting browsers or other environments, consider using a different Babel setup or bundler-specific dynamic import solutions (e.g., Webpack's `import()` for code splitting).","cause":"This plugin specifically transpiles `import()` to `require()` for Node.js environments. If the transpiled code is run in a non-Node.js environment (e.g., a browser without a CommonJS shim like Webpack's), `require` will be undefined.","error":"ReferenceError: require is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null}