import/no-nodejs-modules

Disallow importing Node.js builtin modules such as fs and path

Also called import-x/no-nodejs-modules

Browsers don't have an fs, path or crypto module. Importing one either breaks the bundle or pulls in a large polyfill. Turn this rule on for the files that ship to the client.

It checks import, export ... from, import() and require(), with and without the node: prefix.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"import/no-nodejs-modules" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"import/no-nodejs-modules" = ["error", {
  allow = [], # list of strings
}]

Examples#

example.js3 problems
import fs from "fs";
import { join } from "node:path";

const os = require("os");
example.jsNo problems
import { format } from "date-fns";
import { cart } from "./cart";

Options#

OptionDefaultWhat it does
allow[]Builtin modules you can import, spelled the way the import spells them. "path" doesn't allow "node:path".
example.jswith { "allow": ["node:path"] }No problems
import { join } from "node:path";
example.jswith { "allow": ["node:path"] }1 problem
import { join } from "path";