enforce-node-protocol-usage
Added in v0.9.4Configuration
Rule Details
Enforce consistent use of the node: prefix for Node.js builtin modules.
The rule checks import declarations, named re-exports, dynamic imports and
non-optional calls to require with exactly one string literal argument.
It automatically adds or removes the prefix, preserving the surrounding quotes.
Template literals, require.resolve, TypeScript import types and import-equals
declarations are not checked. Like upstream, the rule also ignores export *
and export * as name declarations.
Options
One string option is required; there is no default:
"always": require the prefix when both the bare and prefixed module exist."never": remove the prefix when the bare module exists. Modules available only with the prefix, such asnode:test, keep it.
always
Examples of incorrect code for this rule with the "always" option:
Examples of correct code for this rule with the "always" option:
never
Examples of incorrect code for this rule with the "never" option:
Examples of correct code for this rule with the "never" option:
Settings
Set import/node-version to the Node.js version your project targets. This
controls which modules support the prefix:
Differences from upstream
- Invalid settings produce lint diagnostics. For example,
settings: { 'import/node-version': 'bad' }reports a diagnostic at each checked module reference and applies no fixes from this rule. These diagnostics follow the rule's configured severity and respect disable comments. Upstream throws an exception instead. - The default Node.js version is
22.0.0. ESLint uses the Node.js version running it. For example,import 'fs'with"always"is reported by default in rslint, but not by ESLint running on Node 14.17. Setimport/node-versionexplicitly to use the same target in both tools. - Version numbers have an upper limit. Each part of
import/node-versionmust be between0and4294967295. For example, rslint rejects4294967296.0.0, which upstream accepts. This does not affect released Node.js versions. - Fixes preserve the module when its prefix contains escapes. With
"never", rslint fixesimport 'node\u{3a}fs'toimport 'fs'. Upstream changes the imported module tou{3a}fsinstead.