Categories
Node.js Quick Tips Quick Tips

Make your imports clearer with the node: protocol

< 1 min read

Last updated: March 31, 2022

This blog post is part of the series What’s new in Node.js core?

It can be hard to tell from just looking at code whether a module that’s being imported is a built in Node.js module, or a third-party dependency. Who can remember the names of all the core modules? Certainly not me!

You can use the node: protocol to make it clear in your code when you’re importing built in Node.js modules. It makes it much easier then to see the modules which are being imported from third-party dependencies.

Example

/**
 * Using the `node:` protocol with `import`.
 *
 * Added: v14.13.1
 * Backported: v12.20.0
 */

import url from "node:url";

/**
 * Using the `node:` protocol with `require()`.
 *
 * Added: v16.0.0
 * Backported: v14.18.0
 */

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

Support in Node.js

  • Added: v14.13.1 (import), v16.0.0 (require)
  • Backported: v12.20.0 (import), v14.18.0 (require)

When v12 goes End-of-life on 30th April 2022, all Current and LTS release lines (v14, v16, v18) will have support for the node: protocol when using import or require().