Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

@typescript-eslint/no-require-imports

Disallow require() in favor of import

In a TypeScript file, a call to require() returns any, so you lose checking and completion for everything you take from the module. The call can also sit anywhere in the code, which hides what the file depends on. An import is typed and sits at the top level.

import name = require("..") is reported too. A require that the file declares itself isn't.

This rule is off by default. Bun runs require() and import in the same file, and some code has no other way to write it, like plugins: [require("tailwindcss-animate")] in a Tailwind config or <Image source={require("./logo.png")} /> in React Native.

Configure

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-require-imports" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"@typescript-eslint/no-require-imports" = ["error", {
  allow = [], # list of strings
  allowAsImport = false,
}]

Examples

Reported:

const path = require("node:path");
import fs = require("node:fs");

console.log(path.join("a", "b"), fs.existsSync("a"));

Not reported:

import path from "node:path";
import * as fs from "node:fs";

console.log(path.join("a", "b"), fs.existsSync("a"));
import { createRequire } from "node:module";

const require = createRequire(import.meta.url);
const addon = require("./addon.node");

Options

OptionDefaultWhat it does
allow[]Regular expressions. A require() of a path that matches one of them is allowed.
allowAsImportfalseAllow import name = require("..").

Not reported with allow: ["/package\\.json$"]:

const { version } = require("../package.json");

With allowAsImport, the first line is allowed and the second is still reported:

import fs = require("node:fs");
const path = require("node:path");