I was very confused with that rule when I recently ported the Ng code base to Nx 12.x. I hope this post helps others who begin migrating from Ng to Nx.
The code base above is a rather small single repo which is now used in production. When using Nx it's a good practice to follow the recommendations for monorepo to be able to use the monorepo benefits in the future as the code base is growing. (E.g. here I'm avoiding the overexposing of the code in the current repo).
I put the code base above into my-org/apps/my-small-repo
. By linting I was confused by the failure of the rule @nrwl/nx/enforce-module-boundaries
. So I tried different possibilities of mapping the src/app
of my-org/apps/my-small-repo
where either compiler or linter or both just failed.
I figured out the following solutions.
Just put
"compilerOptions": {
"baseUrl": "src"
},
into the root of apps/my-small-repo/tsconfig.json
and replace all of your imports inside of apps/my-small-repo
with imports beginning with app
.
Example for a DashboardComponent
:
import { DashboardComponent } from 'app/components/dashboard/dashboard.component';
This solution is tested on nx 13.x, but it probably works on previous versions of nx also.
Put
"app/*": ["apps/my-org/src/app/*"]
to the paths
in compilerOptions
of your tsconfig.base.json
in the repo root. Then put "allowCircularSelfDependency": true,
to the rule @nrwl/nx/enforce-module-boundaries
in the repo root.
We decided for "allowCircularSelfDependency": true,
to avoid working with ugly relative paths like like e.g. this one ../../../../../
in the app. And we also want to have library namespaces in tsconfig.base.json
only.
https://github.com/nrwl/nx/blob/master/packages/eslint-plugin-nx/src/rules/enforce-module-boundaries.ts
For those who are coming here without this getting resolved. (nx monorepo usage)
Trouble shooting the 2 errors (TS error and lint error):
First the Alias error:
Cannot find module '@account/components/something' or its corresponding type declarations.
"compilerOptions":{
...
baseUrl:"." // Try "src" as well incase of boiler plates or if your resolved path (on the error) is missing an src.
path: {
"@account/*": ["app/*"],
"@account/components/*": ["app/components/*"]
}
},
The above will resolve:
import { authMiddleware } from '@account/components/something';
from
import { authMiddleware } from '../../../components/something';
For lint error:
Projects should use relative imports to import from other files within the same project - eslint rule @nrwl/nx/enforce-module-boundaries fails`
"@nrwl/nx/enforce-module-boundaries": [
"error",
{
"allowCircularSelfDependency": true, -> This may solve the lint error.
"allow": ["@account/**"], -> // White list the lint error.
...
}
"@nrwl/nx/enforce-module-boundaries": [
"error",
{
"allow": ["@account/**"], -> // White list the lint error.
...
}
That should fix it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With