Registry / web-framework / babel-plugin-dynamic-import-node-sync

babel-plugin-dynamic-import-node-sync

JSON →
library2.0.1jsnpmunverified

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.

npm install babel-plugin-dynamic-import-node-sync
INSTALL
IMPORT
SIG · BABEL-PLUGIN-DYNAM
B
babel-plugin-dynamic-import-node-sync
web-frameworkjavascriptv2.0.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Demonstrates installation and usage: configuration in .babelrc and compilation result showing import() converted to require().

// .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; }
Debug
Known issues
gotchaThis 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.
affects: >=2.0
gotchaThe 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.
affects: >=2.0
gotchaThis 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.
affects: >=2.0
Errors
Common errors & fixes
Error: Cannot find module '@babel/core'
Missing Babel core package.
fix
Run npm install --save-dev @babel/core
Error: Requires Babel "7.0.0-0" but "@babel/core" is "6.x".
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.
SyntaxError: Unexpected token import
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.
Upgrade
Version history
2.0.1latest on npm
Audit
Dependencies
@babel/plugin-syntax-dynamic-importoptionalRequired by Babel for parsing dynamic import syntax; not a direct dependency but typically needed in Babel config.
Agent activity
11 hits · last 30 days
node
10
OpenAI (training)
1
Resources
babel-plugin-dynamic-import-node-sync — npm install babel-plugin-dynamic-import-node-sync · libregistry