babel-plugin-dynamic-import-node-sync
raw JSON → 2.0.1 verified Sat Apr 25 auth: no javascript
Babel 7 plugin that transpiles dynamic import() expressions to synchronous require() calls for Node.js, primarily used for server-side rendering. Current stable version is 2.0.1, with a single release on Babel 7. Unlike the alternative babel-plugin-dynamic-import-node which wraps require() in Promise.resolve().then(), this plugin outputs a direct synchronous require(), eliminating the Promise wrapper. It matches the proposed import() spec. Installation via npm, configuration via .babelrc or CLI. Designed for Node.js environments where synchronous module loading is acceptable.
Common errors
error Error: Cannot find module '@babel/core' ↓
cause Missing Babel core package.
fix
Run npm install --save-dev @babel/core
error Error: Requires Babel "7.0.0-0" but "@babel/core" is "6.x". ↓
cause Using Babel 6 core with a Babel 7 plugin.
fix
Upgrade to @babel/core 7.x or use version 1.0.1 of this plugin for Babel 6.
error SyntaxError: Unexpected token import ↓
cause Babel is not configured to parse dynamic imports; missing plugin-syntax-dynamic-import.
fix
Add '@babel/plugin-syntax-dynamic-import' to your Babel plugins list, or rely on @babel/preset-env which includes it.
Warnings
gotcha This plugin replaces dynamic import() with require() making it synchronous. If the module has side-effects that expect asynchronous behavior (e.g., network requests), they will execute synchronously and block the event loop. ↓
fix Ensure that the imported module does not rely on asynchronous operations for initialization; use only for modules that are safe to load synchronously, typically local modules or statically analyzable dependencies.
gotcha The plugin transpiles import() to require() regardless of whether the module is ESM or CJS. For ESM modules, require() may fail if the module is not properly compiled to CommonJS or if it uses top-level await. ↓
fix Ensure all imported modules are CommonJS-compatible or transpiled with @babel/plugin-transform-modules-commonjs before using this plugin. Alternatively, use babel-plugin-dynamic-import-node if you need asynchronous behavior.
gotcha This plugin is designed for Node.js server-side rendering only. Do not use in browser bundles, as require() is not available in browsers. ↓
fix Use babel-plugin-dynamic-import-webpack or other browser-compatible dynamic import plugins for client-side code.
Install
npm install babel-plugin-dynamic-import-node-sync yarn add babel-plugin-dynamic-import-node-sync pnpm add babel-plugin-dynamic-import-node-sync Quickstart
// .babelrc
{
"plugins": ["dynamic-import-node-sync"]
}
// code.js
async function loadModule() {
const module = await import('./mymodule');
return module.default;
}
// compiles to:
function loadModule() {
const module = require('./mymodule');
return module.default;
}