Dynamic import() loads a module at runtime and returns a promise that resolves to the module namespace object. In Node.js you use it to load ES modules from CommonJS code, to load code only when needed, and to choose a module by a computed name.
Unlike a static import statement, which must sit at the top level of an ES module, import() is an expression. It works in both CommonJS files and ES modules, inside functions, conditions and loops.
import(specifier) takes a string and returns a promise:
The resolved value is a module namespace object. Named exports become properties, and the default export is the default property. That is why the example destructures default.
A CommonJS file cannot use a static import, but it can call import(). This is the classic way to use an ESM-only package from older code:
CommonJS has no top-level await, so the call must live in an async function or use .then(). Recent Node.js versions can also require() synchronous ES modules, but dynamic import() works in every supported version and remains the portable choice.
Dynamic import lets you delay the cost of loading a heavy module until it is needed:
The report.mjs file is only loaded and evaluated when that command runs. This shortens start-up time for command-line tools. Once loaded, the module is cached, so a second import() of the same URL is fast and does not run the module again.
Because the argument is any expression, you can load modules by name:
Use pathToFileURL() for absolute file paths. On Windows, a path such as C:\app\x.mjs is not a valid specifier, and Node.js rejects it. Never build the specifier from untrusted user input, since that lets a caller run arbitrary code.
JSON modules need an import attribute:
A failing import rejects the promise, so handle it:
| Feature | Static import | Dynamic import() |
|---|---|---|
| Position | Top level of an ES module | Anywhere |
| Specifier | Fixed string | Any expression |
| Returns | Bindings | Promise of a namespace object |
| Works in CommonJS | No | Yes |
| Live bindings | Yes | Yes, through the namespace object |
| Best for | Normal dependencies | Lazy, conditional or computed loads |
Prefer static imports for dependencies you always need, because tools can analyse them and tree-shake unused code.
Yes. It is the standard way to load an ES module from CommonJS. Call it inside an async function because CommonJS has no top-level await.
No. The module is evaluated once and cached by its resolved URL. Later calls return the same namespace object.
The result is the whole namespace object. The default export is stored under the default key, so use mod.default or destructure it.
Convert the path with pathToFileURL(path).href and pass that string to import().
Dynamic import() is an expression that returns a promise of a module namespace object. Use it to load ES modules from CommonJS, to delay heavy modules, and to pick modules by name. Remember .default, use file URLs for absolute paths, add with { type: 'json' } for JSON, and never pass untrusted input as the specifier.