Load Node.js builtin modules

A slightly special import-syntax for builtin modules

Tom • Tools & Development, Learning •

A URL scheme for builtin modules

If you want to import modules in Node.js that are builtin, which means you don’t have to add them to your package.json, then there’s a special URL scheme you can use. Remember that all imports in Node.js are basically URLs that get resolved to paths. Using the “node”-prefix in such imports marks the statement as an import for a builtin module.

The following example uses the ES module system. The import of builtin-modules can also be accomplished with CommonJS-imports, but I strongly suggest using the modern ES module system. It’s stable since Node.js version 15.

// An import of a built-in module,
// in this case 'fs', using the 
// the variant w/promises instead
// of callbacks.
import fs from 'node:fs/promises';

// ... Using 'fs. ...'

javascriptLanguageCheck out all coding posts

As you can see, the change is really small, but clearly marks the import as a builtin module.

Actions

Feedback

Suggested Posts