ES Modules and Dynamic Import
Use import and export in .mjs files or when package.json has type module. In CommonJS you can still load an ES module with dynamic import().
Example
import('node:path').then(({ default: path }) => {
console.log(path.basename('/a/b.txt'));
});
console.log(typeof require, typeof module.exports);Result
function object b.txt
Exercise
Load a module on demand.
("node:os")Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.