import{ name1, name2 }from"./module.js";import{ name as alias }from"./module.js";
Example
import{ areaOfCircle, circumference as circ }from"./mathUtils.js";console.log(areaOfCircle(10));console.log(circ(10));
Note Imports are live bindings -- they reflect the current value in the exporting module. They are also read-only; you cannot reassign an imported binding.
named importimport functionimport from modulerename import
Note Only one default export per module. Can be imported with any name. You can combine default and named imports: import Logger, { version } from "./logger.js".
Note Returns a promise that resolves to the module namespace. Ideal for code-splitting, lazy loading, and conditional imports. Works at runtime, not just at the top of a file.
Note Barrel files simplify imports but can hurt tree-shaking if not handled carefully. Modern bundlers mitigate this, but be aware of the tradeoff.
re-exportbarrel fileexport fromaggregate exports
import.meta
Syntax
import.meta.urlimport.meta.resolve(specifier)
Example
// Get the URL of the current moduleconsole.log(import.meta.url);// "file:///project/src/utils.js"// Resolve a relative pathconst configPath =newURL("./config.json",import.meta.url);console.log(configPath.href);
Note import.meta provides module-specific metadata. In Node.js, use import.meta.dirname and import.meta.filename (Node 21+) as replacements for __dirname and __filename.
import.metamodule urlcurrent file pathimport meta url__dirname ESM