Node.js introduces node: protocol imports

Node.js introduces node: protocol imports

Share

Node.js recently introduced a node: protocol for built-in modules. Built-in node modules can now be imported by prefixing the node: protocol prefix.

So, what previously used to be

import * as fs from 'fs/promises';
JavaScript

can now be imported as

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

Benefits

  • More explicit protocol makes it more readable and also makes it clear that a built-in module is being imported. Since there are a lot of built-in ones, this extra information is useful for beginners or for someone who might not know about one of them.
  • It reduces the risk of a module present in node_modules overriding the newer imports.

It is currently 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)
  • In TypeScript by the latest versions of @types/node.
0
Would love your thoughts, please comment.x
()
x