@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")Require statement not part of import statement.;
const join = require("node:path")Require statement not part of import statement..join;
register(require("./plugin")Require statement not part of import statement.);example.tsNo problems
import fs from "node:fs";
import { join } from "node:path";
import legacy = require("./legacy");
require("./setup");Options#
| Option | Default | What it does |
|---|---|---|
allow | [] | Regular expressions. A require() of a path that matches one of them is allowed. |
example.tswith
{ "allow": ["/package\\.json$"] }No problemsconst pkg = require("../package.json");example.tswith
{ "allow": ["/package\\.json$"] }1 problemconst data = require("../data.json")Require statement not part of import statement.;