Add external imports whitelist logic to nx-enforce-module-boundaries (i.e. allowedExternalImports option)
See original GitHub issue- I’d be willing to implement this feature (contributing guide)
Description
nx-enforce-module-boundaries should allow a deny-all + whitelist logic in addition to the blacklist logic.
Motivation
There are use cases where users might need more restrictive rules and only whitelist some external imports instead of explicitly banning them.
Here is one, when implementing something similar hexagonal, onion, or clean architecture, it is safer to use whitelists.
For instance, a library tagged as “type:core” will not be able to import anything using something like this allowedExternalImports: [], while a “platform:backend” & “type:data-access” will be allowed to use libraries that can communicate with databases or remote services: allowedExternalImports: ['mongo', '@google-cloud/*', ...] or something like this.
Suggested Implementation
We could implement this using an allowedExternalImports or whitelistedExternalImports option.
-
allowedExternalImportsis exclusive withbannedExternalImports. We can’t use both on the same rule. - Nice to have: It would be nice to figure out a way of combining rules. If the project matches multiple rules, then the allowed imports should be merged together
Alternate Implementations
Meanwhile, here is the workaround:
"bannedExternalImports": [
"((?!(@lib-a/*|@lib-b/something|lib-c)).)+"
]
Special thanks @meeroslav for the hint 😉
cf. https://github.com/nrwl/nx/issues/6727#issuecomment-948856631 cc. @mlebarron
Issue Analytics
- State:
- Created a year ago
- Comments:8 (8 by maintainers)

Top Related StackOverflow Question
Hey @meeroslav! I finally managed to take some time and send a PR https://github.com/nrwl/nx/pull/13891 🎉
I was about to send a distinct PR for the docs as I thought that this would require a major rewrite of the “Ban External Imports” recipe but I managed to just add a chapter at the end with some examples.
Let me know what you think of this and if you want me to squash everything.
Good job for the
allSourceTagsbtw 😉True, it doesn’t matter in the opposite direction. Sorry for the confusion