Skip to content
jshacks.dev
Tooling

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.

1 min read
Summarize in AI

Boundaries stay real only if a tool checks them. A restricted-import rule turns "do not import from that package" into a lint error.

packages/ui/eslint.config.jsJavaScript
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.',
            },
          ],
        },
      ],
    },
  },
];
packages/ui/src/button.jsJavaScript
import { db } from '@acme/api';
Text
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-imports

patterns 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.

Share

More snippets

Tooling

Create a Vite project from a template

Create a Vite project from a template with npm create vite, choose the vanilla-ts or react-ts template and see which files it generates.

1 min read
Tooling

Configure VS Code settings for a team

Share VS Code settings and recommended extensions with a team by committing a .vscode folder with settings.json and extensions.json.

1 min read
Tooling

Share an ESLint config across packages

Share one ESLint 9 flat config across workspace packages by exporting an array from a package and spreading it into each package's eslint.config.js.

1 min read

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.