modules

Two systems exist Pick one per project and stay with it

ESM

import { format } from "node:util";

console.log(typeof format);

Output:

function

ESM is the standard type: "module" in package.json opts a project into it Extensions in relative imports are required

CommonJS

const util = require("node:util");

CommonJS is still everywhere in older packages require is synchronous and loads at call time Mixing the two in one file creates confusing failures

The node prefix

import { readFile } from "node:fs/promises";

The node: prefix means core module It cannot be shadowed by a package named fs That is not a style preference, it is a correctness fix

Loading once

Modules are cached after the first import A module with side effects runs once Put configuration inside a function if you need it re-evaluated

Next

Dependencies in 03 packages

Posted