For AI agents: the complete documentation index is available at https://rslint.rs/llms.txt, the full documentation bundle is available at https://rslint.rs/llms-full.txt, and this page is available as Markdown at https://rslint.rs/rules/eslint/no-restricted-imports.md.
close

no-restricted-imports

Added in v0.4.2

Configuration

rslint.config.ts
import { defineConfig, js } from '@rslint/core';

export default defineConfig([
  js.configs.recommended,
  {
    rules: {
      'no-restricted-imports': 'error',
    },
  },
]);

Rule Details

Disallow specified modules when loaded by import. This rule allows you to specify imports that you don't want to use in your application.

This can be useful if you want to restrict usage of certain modules, enforce alternatives, or prevent accidental use of deprecated APIs.

Examples of incorrect code for this rule with ["error", "fs"]:

import fs from 'fs';
export * from 'fs';

Examples of correct code for this rule with ["error", "fs"]:

import crypto from 'crypto';

Options

The rule accepts either an array of strings/objects or an object with paths and patterns properties.

String format

{
  "no-restricted-imports": ["error", "fs", "path"]
}

Object format with paths and patterns

{
  "no-restricted-imports": [
    "error",
    {
      "paths": [
        {
          "name": "import-foo",
          "importNames": ["Bar"],
          "message": "Please use Bar from /import-bar/ instead."
        }
      ],
      "patterns": [
        {
          "group": ["import1/private/*"],
          "message": "usage of import1 private modules not allowed."
        }
      ]
    }
  ]
}

Path options

  • name (required): The module name to restrict
  • message: Custom message to display
  • importNames: Restrict specific named exports
  • allowImportNames: Allow only specified named exports
  • allowTypeImports: Allow type-only imports (TypeScript)

Pattern options

  • group: Gitignore-style patterns
  • regex: Regular expression pattern
  • message: Custom message to display
  • caseSensitive: Case-sensitive matching (default: false)
  • importNames: Restrict specific named imports
  • importNamePattern: Regex pattern for import names
  • allowImportNames: Allow only specified named imports
  • allowImportNamePattern: Regex pattern for allowed import names
  • allowTypeImports: Allow type-only imports (TypeScript)

Differences from upstream

  • Bracket expressions and backslash escapes in patterns[].group: Rslint follows Git glob rules for bracket negation, POSIX character classes, and backslash quoting. For example, group: ["pkg/[!a]"] or group: ["pkg/[^a]"] restricts import 'pkg/b' and allows import 'pkg/a' (whereas upstream treats ! and ^ as literal characters inside the brackets), group: ["pkg/[[:digit:]]*"] matches digits, and group: ["pkg/foo\\*"] or group: ["pkg/foo\\?"] matches only the literal * or ? character.
  • Malformed bracket patterns in patterns[].group: Patterns with an unclosed bracket, empty negated class, or descending range (such as pkg/[abc, pkg/[!], pkg/[a/, or pkg/[z-a]) are skipped and match nothing instead of failing the lint run. Upstream throws on patterns such as pkg/[a/ and drops only the invalid range inside classes such as pkg/[z-a0-9]. Escape a literal [ as \[ to match it as text.

Original Documentation