# @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.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

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

Every option, set to its default:

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

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
const path = require("node:path");
import fs = require("node:fs");

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

Not reported:

{/* good */}
```ts
import path from "node:path";
import * as fs from "node:fs";

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

{/* good */}
```ts
import { createRequire } from "node:module";

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

## Options

| Option | Default | What it does |
| --- | --- | --- |
| `allow` | `[]` | Regular expressions. A `require()` of a path that matches one of them is allowed. |
| `allowAsImport` | `false` | Allow `import name = require("..")`. |

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

{/* good {"options": [{"allow": ["/package\\.json$"]}]} */}
```ts
const { version } = require("../package.json");
```

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

{/* bad {"options": [{"allowAsImport": true}], "count": 1} */}
```ts
import fs = require("node:fs");
const path = require("node:path");
```
