import/enforce-node-protocol-usage

Enforce either `node:fs` or `fs` when importing Node.js built-in modules

"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#

Reported:

import fs from "fs";
export { join } from "path";

const os = require("os");
const crypto = await import("crypto");

Not reported:

import 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"]
OptionWhat it does
"always"Report a built-in module that's imported without node:.
"never"Report a built-in module that's imported with node:.
import fs from "node:fs";
import fs from "fs";

Some modules only exist with the prefix, such as node:test and node:sqlite. "never" leaves them alone:

import { test } from "node:test";