New in Node.js: node: protocol imports

[2021-12-12] dev, javascript, nodejs
(Ad, please don’t block)

Node.js now supports a node: protocol for built-in modules.

The new node: protocol  

Previously:

import * as fs from 'fs/promises';

Now:

import * as fs from 'node:fs/promises';

Benefits of node: imports  

What are the benefits of using node: module specifiers?

  • It’s immediately clear that a built-in Node.js module is imported. Given how many of them there now are, that’s useful information.
  • There is no risk of a module in node_modules overriding the built-in module.
    • This is especially important whenever Node.js adds a new built-in module.

Support for node: imports  

  • Supported in Node.js starting:
    • v16.0.0, v14.18.0 (ESM import and CommonJS require())
    • v14.13.1, v12.20.0 (only ESM import)
  • Supported in TypeScript by the latest versions of @types/node.

More material