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"),
  retries = 3;
example.jsNo problems
const fs = require("fs"),
  join = require("path").join;

const retries = 3;

Options#

OptionDefaultWhat it does
groupingfalseAlso report a declaration that mixes kinds of modules: Node.js built-ins, packages, files (./, ../, /), and computed names.
allowCallfalseCount 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 problem
const fs = require("fs"),
  express = require("express"),
  routes = require("./routes");
example.jswith { "grouping": true }No problems
const 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 problem
const fs = require("node:fs"),
  path = require("path");

Without allowCall, a module that's called counts as an ordinary variable:

example.js1 problem
const express = require("express"),
  debug = require("debug")("app");
example.jswith { "allowCall": true }No problems
const express = require("express"),
  debug = require("debug")("app");