prefer-node-protocol
UnreleasedConfiguration
Requires the node: prefix when importing Node.js built-in modules.
Rule details
The rule checks static imports, re-exports, dynamic imports, require(), and
process.getBuiltinModule(), including globalThis.process.getBuiltinModule().
Automatic fixes insert node: while preserving quotes, escapes and comments.
Examples of incorrect code:
Examples of correct code:
Relative paths, third-party packages, template literals, require.resolve(),
and optional require?.() calls are ignored. Names available only with the
prefix, such as node:test, do not make the bare name test a built-in module.
Calls on locally defined require, process, or globalThis are ignored
unless they are recognized Node bindings. Direct imports or require() loads
of process and require functions initialized by an imported
module.createRequire() are recognized; arbitrary local aliases and custom
wrappers are not followed.
Options
The optional object accepts version, a Node.js version range:
The target range comes from the first valid value in this order:
- The rule's
versionoption. settings.n.version, thensettings.node.version.- The nearest package's
engines.node. - Its
devEngines.runtimeentry namednode. >=16.0.0.
Imports and re-exports are checked only when the entire target range supports
the prefix: ^12.20.0 || >=14.13.1. require() additionally needs
^14.18.0 || >=16.0.0. For example, >=14.18.0 includes Node 15, so it enables
checks for imports but not for require(). process.getBuiltinModule() is
always checked because that API implies support for the prefix.
Differences from upstream
- Custom local bindings are left unchanged. For example,
function require(name) { return name; } require("fs")is not reported, because adding the prefix would change the returned value. Upstream fixes calls based on their spelling even when the name refers to a custom function. - Contradictory alternatives do not affect a version range. Both
>=16 || >20 <16and>20 <16 || >=16enable fixes for imports andrequire(), because>20 <16contains no versions. Upstream disables these checks for the first ordering.