---
title: Enforce Module Boundaries Rule
description: Learn how to use the @nx/enforce-module-boundaries lint rule on ESLint or Oxlint to define strict rules for accessing resources between different projects in your Nx workspace.
sidebar:
  label: The enforce-module-boundaries rule
filter: 'type:Guides'
topics:
  - 'ESLint'
---

The `@nx/enforce-module-boundaries` lint rule enables you to define strict rules for accessing resources between
different projects in the repository. Enforcing strict boundaries helps to prevent unplanned cross-dependencies.

{% aside type="note" %}
This rule only works for JavaScript/TypeScript projects. It runs on ESLint, and experimentally on [Oxlint](/docs/technologies/oxlint/introduction). For language-agnostic boundary enforcement across all project dependencies, see the [Conformance plugin's Enforce Project Boundaries rule](/docs/enterprise/conformance).
{% /aside %}

## Usage

Add the rule to your linter configuration:

{% tabs syncKey="eslint-config-preference" %}
{% tabitem label="Flat Config" %}

```javascript
// eslint.config.mjs
import nxPlugin from '@nx/eslint-plugin';

export default [
  ...nxPlugin.configs['flat/base'],
  ...nxPlugin.configs['flat/typescript'],
  ...nxPlugin.configs['flat/javascript'],
  {
    files: ['**/*.ts', '**/*.tsx', '**/*.js', '**/*.jsx'],
    rules: {
      '@nx/enforce-module-boundaries': [
        'error',
        {
          // ...rule specific configuration
        },
      ],
    },
  },
];
```

{% /tabitem %}
{% tabitem label="Legacy (.eslintrc.json)" %}

```jsonc
// .eslintrc.json
{
  // ... more ESLint config here
  "overrides": [
    {
      "files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
      "rules": {
        "@nx/enforce-module-boundaries": [
          "error",
          {
            // ...rule specific configuration
          },
        ],
      },
    },
    // ... more ESLint overrides here
  ],
}
```

{% /tabitem %}
{% tabitem label="Oxlint" %}

```json
// .oxlintrc.json
{
  "jsPlugins": ["@nx/oxlint/boundaries-plugin"],
  "rules": {
    "@nx/enforce-module-boundaries": [
      "error",
      {
        // ...rule specific configuration
      }
    ]
  }
}
```

{% /tabitem %}
{% /tabs %}

## Options

| Property                           | Type                      | Default | Description                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
| ---------------------------------- | ------------------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| allow                              | _Array<string>_           | _[]_    | List of imports that should be allowed without any checks                                                                                                                                                                                                                                                                                                                                                                                                                                                                               |
| allowCircularSelfDependency        | _boolean_                 | _false_ | Disable check for self circular dependency when project imports from itself via alias path                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| banTransitiveDependencies          | _boolean_                 | _false_ | Ban import of dependencies that were not specified in the root or project's `package.json`                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| ignoredCircularDependencies        | _Array<[string, string]>_ | _[]_    | List of project pairs that should be skipped from `Circular dependencies` checks, including the self-circular dependency check. E.g. `['feature-project-a', 'myapp']`. Project name can be replaced by catch all `*` for more generic matches.                                                                                                                                                                                                                                                                                          |
| checkDynamicDependenciesExceptions | _Array<string>_           | _[]_    | List of imports that should be skipped for `Imports of lazy-loaded libraries forbidden` checks. E.g. `['@myorg/lazy-project/component/*', '@myorg/other-project']`                                                                                                                                                                                                                                                                                                                                                                      |
| checkNestedExternalImports         | _boolean_                 | _false_ | Enable to also apply the `bannedExternalImports` and `allowedExternalImports` constraints to the external packages that imported projects depend on, transitively. The nested check compares whole package names. In `bannedExternalImports`, a subpath entry such as `lodash/fp` only applies to direct imports. In `allowedExternalImports`, a subpath entry does not allow the package for the nested check, so the rule reports the nested dependency. Check [Dependency constraints](#dependency-constraints) for more information |
| enforceBuildableLibDependency      | _boolean_                 | _false_ | Enable to restrict the buildable libs from importing non-buildable libraries                                                                                                                                                                                                                                                                                                                                                                                                                                                            |
| depConstraints                     | _Array<object>_           | _[]_    | List of dependency constraints between projects                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         |

### Dependency constraints

The `depConstraints` is an array of objects representing the constraints defined between source and target projects. A
constraint must include `sourceTag` or `allSourceTags`. The constraints are applied with **AND** logical operation - for
a given `source` project the resulting constraints would be **all** that match its tags.

| Property                 | Type            | Description                                                                        |
| ------------------------ | --------------- | ---------------------------------------------------------------------------------- |
| sourceTag                | _string_        | Tag that source project must contain to match the constraint                       |
| allSourceTags            | _Array<string>_ | List of tags the source project must contain to match the constraint               |
| onlyDependOnLibsWithTags | _Array<string>_ | The source **can depend only** on projects that contain at least one of these tags |
| notDependOnLibsWithTags  | _Array<string>_ | The source **can not depend** on projects that contain at least one of these tags  |
| allowedExternalImports   | _Array<string>_ | Exclusive list of external (npm) packages that are allowed to be imported          |
| bannedExternalImports    | _Array<string>_ | List of external (npm) packages that are banned from importing                     |

## E2E tests that hash from their imports

An atomized e2e test file with `// @nx-ignore-server-inputs` imports app code so its task
[hashes from that code](/docs/kb/skip-unaffected-e2e-tests#using-nx-ignore-server-inputs).
For its bare imports of other projects, such as
`import '../../shop/src/app/checkout/checkout-page'`, the rule skips these checks:

- Relative and absolute imports.
- Imports of apps.
- Static imports of lazy-loaded libraries.
- `depConstraints`.

It still checks imports of e2e projects, circular dependencies, and direct imports of external
packages.
Imports that name values or types get the usual checks.
A test file with only an `@nx-depends-on` directive gets every check.
A file counts as atomized when its project has a target named `<target>--<file path>`, with the
file path relative to the project root, such as `e2e-ci--src/checkout.spec.ts`.

Read more about the proper usage of this rule:

- [Enforce Module Boundaries](/docs/features/enforce-module-boundaries)
- [Ban Dependencies with Certain Tags](/docs/guides/enforce-module-boundaries/ban-dependencies-with-tags)
- [Tag in Multiple Dimensions](/docs/guides/enforce-module-boundaries/tag-multiple-dimensions)
- [Ban External Imports](/docs/guides/enforce-module-boundaries/ban-external-imports)
- [Tags Allow List](/docs/guides/enforce-module-boundaries/tags-allow-list)
- [Taming Code Organization with Module Boundaries in Nx](https://nx.dev/blog/mastering-the-project-boundaries-in-nx)
