no-restricted-imports

Disallow importing the modules, or the names from them, that you list

A project often has modules that some code shouldn't import, like a package that's been replaced, a Node.js module in code that also runs in the browser, or another package's internal files. This rule reports an import of them, and an export ... from as well.

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

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"no-restricted-imports" = "error" # or "warn"

Every option, set to its default. The ones that are commented out are unset by default.

bunfig.toml
[lint.rules]
"no-restricted-imports" = ["error", {
  # paths = <list of strings or objects>,
  # patterns = <list of strings or list of objects>,
}]

Examples#

bunfig.toml
[lint.rules]
no-restricted-imports = ["error", "moment", {
  name = "lodash",
  message = "Use lodash-es.",
}]
example.jswith "moment", { "name": "lodash", "message": "Use lodash-es." }2 problems
import moment from "moment";
export { merge } from "lodash";
example.jswith "moment", { "name": "lodash", "message": "Use lodash-es." }No problems
import { format } from "date-fns";
export { merge } from "lodash-es";
import debounce from "lodash/debounce";

const moment = await import("moment");

A name must match the whole string, so "lodash" doesn't cover "lodash/debounce". Use a pattern for that.

Options#

Each option is a module name or an object from the paths table. To use patterns, pass one object with paths, patterns or both:

bunfig.toml
[lint.rules]
no-restricted-imports = ["error", {
  paths = ["moment"],
  patterns = ["lodash/*"],
}]

paths#

KeyWhat it does
nameThe module, exactly as it's written in the import. Required.
messageText added to the report.
importNamesReport only these names from the module. "default" is the default import.
allowImportNamesReport every name from the module except these.
allowTypeImportstrue allows import type and export type. Default false.

With { name: "react", importNames: ["useLayoutEffect"] }, useState is fine. The * is reported because it gives access to the name:

example.jswith { "paths": [{ "name": "react", "importNames": ["useLayoutEffect"] }] }2 problems
import { useLayoutEffect, useState } from "react";
import * as React from "react";

With { name: "./database", allowTypeImports: true }:

example.tswith { "paths": [{ "name": "./database", "allowTypeImports": true }] }No problems
import type { Row } from "./database";

patterns#

A list of strings in .gitignore syntax, where a pattern that starts with ! makes an exception to the ones before it. With an oxlint config they're globs that must match the whole module name.

Or a list of objects:

KeyWhat it does
groupA list of .gitignore patterns. Each object needs a group or a regex.
regexA regular expression that's tested against the module name.
caseSensitivetrue makes group or regex case sensitive. Default false.
importNames, allowImportNamesAs in paths.
importNamePatternReport only the names that match this regular expression.
allowImportNamePatternReport every name except the ones that match this regular expression.
message, allowTypeImportsAs in paths.

With patterns: ["@acme/billing/*", "!@acme/billing/testing"], this is reported:

example.jswith { "patterns": ["@acme/billing/*", "!@acme/billing/testing"] }1 problem
import { ledger } from "@acme/billing/internal";

And this isn't:

example.jswith { "patterns": ["@acme/billing/*", "!@acme/billing/testing"] }No problems
import { charge } from "@acme/billing";
import { fakeCard } from "@acme/billing/testing";

With patterns: [{ regex: "^\\.\\./\\.\\./", message: "Import from the package root." }]:

example.jswith { "patterns": [{ "regex": "^\\.\\./\\.\\./", "message": "Import from the package root." }] }1 problem
import { parse } from "../../shared/parse.js";

With patterns: [{ group: ["./icons/*"], importNamePattern: "^Legacy" }], only LegacyArrow is reported:

example.jswith { "patterns": [{ "group": ["./icons/*"], "importNamePattern": "^Legacy" }] }1 problem
import { LegacyArrow, Arrow } from "./icons/arrows.js";