Search code examples
javascriptnode.jswebpackchalk

What's the difference between node:process and process?


When I import node:process it works fine. However, when I try to require the same, it gives an error.

This works fine:

import process from 'node:process';

But when I try to require the same, it throws an error:

const process = require('node:process');

Error: Cannot find module 'node:process'

I am curious as to what is the difference between process, which works in both, commonjs and module, vs node:process.

Also, a follow-up, I am using webpack to bundle my js, and I discovered this error when I tried to run my bundled code and realised, that chalk imports node:process, node:os and node:tty. How do I solve that now?


Solution

  • "node:" is an URL scheme for loading ECMAScript modules. As such it started for "import", not "require".

    "node:process" is just an alternative name to load the built-in "process" module.

    See also Node.js documentation - you can find the lowest supporting Node.js version inside the "History" tag (12.20.0, 14.13.1)

    With newer Node.js it should be available for "require" as well (14.18.0, 16.0.0).

    Some more details can be found here: node:process always prefers the built-in core module, while process could be loaded from a file.