Enforce import boundaries in a monorepo
Enforce import boundaries between monorepo packages with ESLint's no-restricted-imports rule, so a UI package cannot import from an API package.
Boundaries stay real only if a tool checks them. A restricted-import rule turns "do not import from that package" into a lint error.
export default [
{
files: ['src/**/*.js'],
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
{
group: ['@acme/api', '@acme/api/*'],
message: 'UI code must not import server code. Move shared types to @acme/types.',
},
],
},
],
},
},
];import { db } from '@acme/api';packages/ui/src/button.js
1:1 error '@acme/api' import is restricted from being used by a pattern. UI code must not import server code. Move shared types to @acme/types. no-restricted-importspatterns takes gitignore-style globs and applies to the module name, so it also blocks deep imports. The message appears in the error, so use it to say what to do instead. See [the no-restricted-imports rule](https://eslint.org/docs/latest/rules/no-restricted-imports).
The rule checks names, not what a package re-exports. For layers inside one package, an architecture-aware tool such as dependency-cruiser is a better fit.