Enforce module boundaries rule

The @nx/enforce-module-boundaries ESLint 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.

Usage

You can use the enforce-module-boundaries rule by adding it to your ESLint rules configuration:

1{ 2 // ... more ESLint config here 3 "overrides": [ 4 { 5 "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], 6 "rules": { 7 "@nx/enforce-module-boundaries": [ 8 "error", 9 { 10 // ...rule specific configuration 11 } 12 ] 13 } 14 } 15 // ... more ESLint overrides here 16 ] 17} 18

Options

PropertyTypeDefaultDescription
allowArray<string>[]List of imports that should be allowed without any checks
allowCircularSelfDependencybooleanfalseDisable check for self circular dependency when project imports from itself via alias path
banTransitiveDependenciesbooleanfalseBan import of dependencies that were not specified in the root or project's package.json
checkDynamicDependenciesExceptionsArray<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']
checkNestedExternalImportsbooleanfalseEnable to enforce the check for banned external imports in the nested packages. Check Dependency constraits for more information
enforceBuildableLibDependencybooleanfalseEnable to restrict the buildable libs from importing non-buildable libraries
depConstraintsArray<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 given source project the resulting constraints would be all that match its tags.

PropertyTypeDescription
sourceTagstringTag that source project must contain to match the constraint
allSourceTagsArray<string>List of tags the source project must contain to match the constraint
onlyDependOnLibsWithTagsArray<string>The source can depend only on projects that contain at least one of these tags
notDependOnLibsWithTagsArray<string>The source can not depend on projects that contain at least one of these tags
allowedExternalImportsArray<string>Exclusive list of external (npm) packages that are allowed to be imported
bannedExternalImportsArray<string>List of external (npm) packages that are banned from importing

Read more about the proper usage of this rule: