useNodejsImportProtocol
¥Summary
-
规则生效日期:
v1.5.0¥Rule available since:
v1.5.0 -
诊断类别:
lint/style/useNodejsImportProtocol¥Diagnostic Category:
lint/style/useNodejsImportProtocol -
此规则为推荐规则,默认启用。
¥This rule is recommended, which means is enabled by default.
-
此规则包含 unsafe 修复程序。
¥This rule has an unsafe fix.
-
此规则的默认严重级别为 information。
¥The default severity of this rule is information.
-
来源:
¥Sources:
-
与
unicorn/prefer-node-protocol相同¥Same as
unicorn/prefer-node-protocol -
与
import/enforce-node-protocol-usage相同¥Same as
import/enforce-node-protocol-usage
-
¥How to configure
{ "linter": { "rules": { "style": { "useNodejsImportProtocol": "error" } } }}¥Description
强制使用 node: 协议来处理 Node.js 内置模块。
¥Enforces using the node: protocol for Node.js builtin modules.
规则将传统导入(如 import fs from "fs";)标记为无效,改为建议使用 import fs from "node:fs"; 格式。
¥The rule marks traditional imports like import fs from "fs"; as invalid,
suggesting the format import fs from "node:fs"; instead.
如果 package.json 中声明的依赖与内置 Node.js 模块的名称匹配,则也不会触发该规则。
¥The rule also isn’t triggered if there are dependencies declared in the package.json that match
the name of a built-in Node.js module.
¥Examples
¥Invalid
import fs from 'fs';code-block.js:1:16 lint/style/useNodejsImportProtocol FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ A Node.js builtin module should be imported with the node: protocol.
> 1 │ import fs from ‘fs’;
│ ^^^^
2 │
ℹ Using the node: protocol is more explicit and signals that the imported module belongs to Node.js.
ℹ Unsafe fix: Add the node: protocol.
1 │ - import·fs·from·‘fs’;
1 │ + import·fs·from·‘node:fs’;
2 2 │
import os from 'os';code-block.js:1:16 lint/style/useNodejsImportProtocol FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ A Node.js builtin module should be imported with the node: protocol.
> 1 │ import os from ‘os’;
│ ^^^^
2 │
ℹ Using the node: protocol is more explicit and signals that the imported module belongs to Node.js.
ℹ Unsafe fix: Add the node: protocol.
1 │ - import·os·from·‘os’;
1 │ + import·os·from·‘node:os’;
2 2 │
import path from 'path';code-block.js:1:18 lint/style/useNodejsImportProtocol FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ A Node.js builtin module should be imported with the node: protocol.
> 1 │ import path from ‘path’;
│ ^^^^^^
2 │
ℹ Using the node: protocol is more explicit and signals that the imported module belongs to Node.js.
ℹ Unsafe fix: Add the node: protocol.
1 │ - import·path·from·‘path’;
1 │ + import·path·from·‘node:path’;
2 2 │
¥Valid
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';¥Related links
Biome v2.1 中文网 - 粤ICP备13048890号