eslint-plugin-strict-dependencies
raw JSON →ESLint plugin to enforce custom module dependency rules in TypeScript projects. Current stable version is 1.3.33, released under MIT license. It uses tsconfig paths for alias resolution and supports glob patterns, forward matching, and per-rule allow-lists for import sources. Unlike generic import lint rules, it allows granular control over which modules can import from which paths, including member-level restrictions (e.g., disallowing `Suspense` from `react` except in a specific file). It also offers options like `resolveRelativeImport` to convert relative imports to absolute paths for linting, and `excludeTypeImportChecks` to skip type-only imports. The plugin is actively maintained with regular dependency updates but no major feature changes recently.
Common errors
error Error: Failed to load plugin 'strict-dependencies' declared in '.eslintrc': Cannot find module 'eslint-plugin-strict-dependencies' ↓
error Error: Cannot find module 'typescript'. Please install 'typescript' in your project. ↓
error Error: Could not find tsconfig.json. Make sure a tsconfig.json file exists in the project root. ↓
error Parsing error: The keyword 'import' is reserved ↓
Warnings
breaking The plugin requires a tsconfig.json file in the project root. Without it, the rule will not work. ↓
gotcha The rule uses tsconfig paths for alias resolution, but by default uses index 0 of each path array. If your tsconfig has multiple paths in an array (e.g., ['aaa/*', 'bbb/*']), the plugin may resolve to the wrong alias. ↓
gotcha Relative imports are not resolved by default; they are excluded from linting. This can cause missed violations where a relative import reaches a disallowed module. ↓
gotcha The 'module' pattern uses glob matching but also supports forward matching (string prefix). This dual behavior may cause unexpected matches if a pattern accidentally matches unintended paths. ↓
deprecated The 'allowSameModule' option defaults to true in v1.x but may be deprecated in future versions to enforce stricter rules. ↓
Install
npm install eslint-plugin-strict-dependencies yarn add eslint-plugin-strict-dependencies pnpm add eslint-plugin-strict-dependencies Imports
- plugin (default) wrong
import { strictDependencies } from 'eslint-plugin-strict-dependencies';correctimport plugin from 'eslint-plugin-strict-dependencies'; // or require('eslint-plugin-strict-dependencies') - strict-dependencies rule wrong
// Incorrect rule name: "strict-dependencies/rule" or "strict-dependencies/strict-dependency"correct// In .eslintrc: "strict-dependencies/strict-dependencies": "error" - TypeScript type for options wrong
import { Options } from 'eslint-plugin-strict-dependencies';correctimport type { Options } from 'eslint-plugin-strict-dependencies';
Quickstart
// .eslintrc.js
module.exports = {
plugins: ['strict-dependencies'],
rules: {
'strict-dependencies/strict-dependencies': [
'error',
[
{
module: 'src/components/ui',
allowReferenceFrom: ['src/components/page'],
allowSameModule: true,
},
{
module: 'next/router',
allowReferenceFrom: ['src/libs/router.ts'],
allowSameModule: false,
},
],
// optional options
// {
// resolveRelativeImport: true,
// pathIndexMap: { '*': 1 }
// }
],
},
};