4

New in Node.js: `node:` protocol imports

 2 years ago
source link: https://2ality.com/2021/12/node-protocol-imports.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
`node:` protocol imports
Black lives matter
Dr. Axel Rauschmayer
Homepage | Twitter
Book, exercises, quizzes
(free to read online)
Book (50% free online)
Book (first part free online)
Newsletter (free)

New in Node.js: node: protocol imports

(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';
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  #


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK