exportconst name = value;
exportfunction fn() { ... }
export { a, b, c };
example
// mathUtils.jsexportconst PI = 3.14159;
exportfunction areaOfCircle(radius) {
return PI * radius ** 2;
}
exportfunction circumference(radius) {
return2 * PI * radius;
}
Note Named exports can be many per module. They must be imported by exact name (or renamed with 'as'). Prefer named exports for better tree-shaking.
named exportexport functionexport variablemodule export
Named Imports
syntax
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.url
import.meta.resolve(specifier)
example
// Get the URL of the current module
console.log(import.meta.url);
// "file:///project/src/utils.js"// Resolve a relative pathconst configPath = new URL("./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