import/enforce-node-protocol-usage
Enforce either node:fs or fs when importing Node.js built-in modules
fixable
Also called import-x/enforce-node-protocol-usage
"node:fs" and "fs" load the same module. With the prefix, you can see right away that the import is a built-in and not a package from node_modules, and no installed package can take its place. Without it, the code also runs on old Node.js versions. This rule keeps a project on one of the two.
It checks import, export { } from, import() and require(). There's no default, so your config must say "always" or "never".
--fix adds or removes the prefix.
Configure#
Off by default. It needs an option:
bunfig.toml
[lint.rules]
# "always" or "never"
"import/enforce-node-protocol-usage" = ["error", "always"]Examples#
example.jswith
"always"4 problemsimport fs from "fs"Prefer node:fs over fs.;
export { join } from "path"Prefer node:path over path.;
const os = require("os"Prefer node:os over os.);
const crypto = await import("crypto"Prefer node:crypto over crypto.);example.jswith
"always"No problemsimport fs from "node:fs";
export { join } from "node:path";
const os = require("node:os");
const crypto = await import("node:crypto");Options#
bunfig.toml
[lint.rules]
"import/enforce-node-protocol-usage" = ["error", "always"]| Option | What it does |
|---|---|
"always" | Report a built-in module that's imported without node:. |
"never" | Report a built-in module that's imported with node:. |
example.jswith
"never"1 problemimport fs from "node:fs"Prefer fs over node:fs.;example.jswith
"never"No problemsimport fs from "fs";Some modules only exist with the prefix, such as node:test and node:sqlite. "never" leaves them alone:
example.jswith
"never"No problemsimport { test } from "node:test";