babel-plugin-dynamic-import-node-babel-7

raw JSON →
2.0.7 verified Sat Apr 25 auth: no javascript

A Babel plugin that transpiles dynamic import() expressions to deferred require() calls for Node.js environments. This version (2.0.7) is a fork of the original airbnb plugin updated to support Babel 7. It matches the proposed import() spec and requires Babylon >= v6.12.0 to correctly parse dynamic imports. Key differentiators: specifically targets Node.js (not browsers), uses deferred require() to preserve lazy loading semantics, and is a drop-in replacement for the Babel 6 version. Maintenance status: a fork created to test Babel 7 compatibility; users should consider the official airbnb version or alternative dynamic import transforms for production.

error Error: Cannot find module 'babel-plugin-dynamic-import-node-babel-7'
cause Plugin not installed or package name misspelled in configuration.
fix
Run npm install --save-dev babel-plugin-dynamic-import-node-babel-7 and check .babelrc for correct name.
error Support for the experimental syntax 'importMeta' isn't currently enabled
cause Babel not configured to parse dynamic import syntax; this plugin only transforms, it doesn't parse.
fix
Add '@babel/plugin-syntax-dynamic-import' to plugins list in addition to this plugin.
deprecated This fork may not receive updates; prefer the original airbnb plugin or @babel/plugin-syntax-dynamic-import with a dynamic import polyfill.
fix Use the official @babel/plugin-syntax-dynamic-import for parsing, and a runtime polyfill like dynamic-import-polyfill if needed.
gotcha The plugin name in .babelrc is 'dynamic-import-node-babel-7', not 'dynamic-import-node' (original plugin name).
fix Ensure the plugin string matches the package name: "dynamic-import-node-babel-7".
breaking Babel 7 changed plugin API; this fork may not be compatible with Babel 6 or future Babel versions.
fix Use only with Babel 7. For Babel 6, use the original `babel-plugin-dynamic-import-node`.
npm install babel-plugin-dynamic-import-node-babel-7
yarn add babel-plugin-dynamic-import-node-babel-7
pnpm add babel-plugin-dynamic-import-node-babel-7

Shows Babel configuration and how dynamic import() is transformed to deferred require() for Node.

// Install: npm install --save-dev babel-plugin-dynamic-import-node-babel-7
// .babelrc
{
  "plugins": ["dynamic-import-node-babel-7"]
}

// Source code
async function loadModule() {
  const module = await import('./some-module');
  return module;
}

// After transpilation (Node)
function loadModule() {
  return Promise.resolve().then(() => require('./some-module'));
}