import/no-nodejs-modules

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

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#

Reported:

import fs from "fs";
import { join } from "node:path";

const os = require("os");

Not reported:

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".
import { join } from "node:path";
import { join } from "path";