# @typescript-eslint/no-restricted-imports

> Disallow imports of the modules you list, with an exception for type imports

Some modules shouldn't be imported in your project, or not from everywhere, like a library you're moving away from, another package's internals, or server code in the browser bundle. You list them, and the rule reports every `import`, `export .. from` and `import x = require("..")` of one.

The rule does nothing until you give it options. Like typescript-eslint, it doesn't check `require()` or `import()`. With an oxlint config it does check `import()`, because oxlint's version of the rule does.

This rule is deprecated. Use `no-restricted-imports` instead. It has the same options, including `allowTypeImports`.

## Examples

```toml bunfig.toml
[lint.rules]
"@typescript-eslint/no-restricted-imports" = ["error", {
  paths = [{ name = "moment", message = "Use Temporal instead." }],
}]
```

Reported:

{/* bad {"count": 3, "options": [{"paths": [{"name": "moment", "message": "Use Temporal instead."}]}]} */}
```ts
import moment from "moment";
import legacy = require("moment");
export { duration } from "moment";
```

Not reported:

{/* good {"options": [{"paths": [{"name": "moment", "message": "Use Temporal instead."}]}]} */}
```ts
import { format } from "./dates";

const moment = await import("moment");
```

## Options

Pass either a list of `paths` entries, such as `["error", "moment", "lodash"]`, or one object with `paths`, `patterns` or both.

Each `paths` entry is a module name or an object:

| Key | What it does |
| --- | --- |
| `name` | The module, exactly as it's written in the import. Required. |
| `message` | Text added to the report. |
| `importNames` | Report only these names. Other names from the module are allowed. |
| `allowImportNames` | Report every name except these. |
| `allowTypeImports` | `true` allows `import type` and `export type` from the module. Default `false`. |

`patterns` is a list of patterns in `.gitignore` syntax, or a list of objects. An object takes the keys `message`, `importNames`, `allowImportNames` and `allowTypeImports` from above, plus these:

| Key | What it does |
| --- | --- |
| `group` | A list of patterns in `.gitignore` syntax. A pattern that starts with `!` makes an exception. |
| `regex` | A regular expression, instead of `group`. |
| `caseSensitive` | `true` makes `group` or `regex` case sensitive. Default `false`. |
| `importNamePattern` | Report only the names that match this regular expression. |
| `allowImportNamePattern` | Report every name except those that match this regular expression. |

An `import type` is erased when the file is compiled, so it doesn't load anything. With `{ name: "./server/db", allowTypeImports: true }`, these are allowed:

{/* good {"options": [{"paths": [{"name": "./server/db", "allowTypeImports": true}]}]} */}
```ts
import type { User } from "./server/db";
import { type Order } from "./server/db";
```

This one also imports a value, so it's reported:

{/* bad {"options": [{"paths": [{"name": "./server/db", "allowTypeImports": true}]}]} */}
```ts
import { query, type User } from "./server/db";
```

With `{ name: "react", importNames: ["useLayoutEffect"] }`, only the first line is reported:

{/* bad {"options": [{"paths": [{"name": "react", "importNames": ["useLayoutEffect"]}]}], "count": 1} */}
```ts
import { useLayoutEffect } from "react";
import { useEffect } from "react";
```

With `patterns: [{ group: ["@shop/billing/*", "!@shop/billing/api"] }]`, only the first line is reported:

{/* bad {"options": [{"patterns": [{"group": ["@shop/billing/*", "!@shop/billing/api"]}]}], "count": 1} */}
```ts
import { charge } from "@shop/billing/stripe";
import { pay } from "@shop/billing/api";
```
