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";Do not import Node.js builtin module fs
import { join } from "node:path";Do not import Node.js builtin module node:path
const os = require("os")Do not import Node.js builtin module os;example.jsNo problems
import { format } from "date-fns";
import { cart } from "./cart";Options#
| Option | Default | What 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 problemsimport { join } from "node:path";example.jswith
{ "allow": ["node:path"] }1 problemimport { join } from "path";Do not import Node.js builtin module path