n/no-mixed-requires
Disallow require() calls mixed with other variables in one declaration
Also called node/no-mixed-requires
A declaration that lists modules and ordinary variables together makes it hard to see what the file depends on. This rule wants the require() calls in their own declaration.
require("x") and a property of it, such as require("x").y, count as a require(). Everything else is an ordinary variable, including one without a value.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"n/no-mixed-requires" = "error" # or "warn"Every option, set to its default:
bunfig.toml
[lint.rules]
"n/no-mixed-requires" = ["error", {
grouping = false,
allowCall = false,
}]Examples#
example.js1 problem
const fs = require("fs"),Do not mix require and other declarations.
retries = 3;example.jsNo problems
const fs = require("fs"),
join = require("path").join;
const retries = 3;Options#
| Option | Default | What it does |
|---|---|---|
grouping | false | Also report a declaration that mixes kinds of modules: Node.js built-ins, packages, files (./, ../, /), and computed names. |
allowCall | false | Count a module that's called right away, such as require("debug")("app"), as a require(). |
You can write true instead of { "grouping": true }.
example.jswith
{ "grouping": true }1 problemconst fs = require("fs"),Do not mix core, module, file and computed requires.
express = require("express"),
routes = require("./routes");example.jswith
{ "grouping": true }No problemsconst fs = require("fs"),
path = require("path");
const express = require("express");
const routes = require("./routes");A name with the node: prefix counts as a package, not as a built-in:
example.jswith
{ "grouping": true }1 problemconst fs = require("node:fs"),Do not mix core, module, file and computed requires.
path = require("path");Without allowCall, a module that's called counts as an ordinary variable:
example.js1 problem
const express = require("express"),Do not mix require and other declarations.
debug = require("debug")("app");example.jswith
{ "allowCall": true }No problemsconst express = require("express"),
debug = require("debug")("app");