Categories
Node.js Quick Tips Quick Tips

Make your imports clearer with the node: protocol

< 1 min read

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 in an ES module.
 *
 * Added: v14.13.1
 * Backported to: 12.x (v12.20.0)
 */

import url from "node:url";

/**
 * Using the `node:` protocol in a CommonJS module.
 *
 * Added: v16.0.0
 * Backported to: None
 */

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

Support

Support in Node.js versions:

  • Added: v14.13.1 (ESM), v16.0.0 (CommonJS)
  • Backported to: 12.x (v12.20.0, ESM only)

Related links

  • Node.js documentation for the node: protocol: ESM, CommonJS

Leave a Reply

Your email address will not be published. Required fields are marked *