@typescript-eslint/no-var-requires

Disallow const x = require() in favor of import

deprecated

Also called typescript-eslint/no-var-requires, typescript/no-var-requires

In a TypeScript file, require() returns any, so const fs = require("node:fs") loses all the module's types. An import keeps them.

The rule reports require() in a variable declaration, and where its result is called, passed to a function, read from with . or [], or given a type with as. A bare require("./setup"); and import fs = require("fs") are allowed, and so is your own function named require.

This rule is deprecated. Use @typescript-eslint/no-require-imports instead.

Examples#

example.ts3 problems
const fs = require("node:fs");
const join = require("node:path").join;
register(require("./plugin"));
example.tsNo problems
import fs from "node:fs";
import { join } from "node:path";
import legacy = require("./legacy");

require("./setup");

Options#

OptionDefaultWhat it does
allow[]Regular expressions. A require() of a path that matches one of them is allowed.
example.tswith { "allow": ["/package\\.json$"] }No problems
const pkg = require("../package.json");
example.tswith { "allow": ["/package\\.json$"] }1 problem
const data = require("../data.json");