nx: Cannot find module with Application specific Tsconfig paths
I have an issue where I want to use set up paths within my tsconfig.app.json which resides within in application (not at the root level of the project).
Ideally I want to set up shortnames for my imports for that application only and not just throw in the tsconfig.json which resides in the root of the workspace.
I dont want to expose paths within my tsconfig.app.json to my entire workspace.
Here is what my tsconfig.app.json looks likes:
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc/apps/configuration",
"types": [],
"baseUrl": "./src",
"paths": {
"@assets/*": [
"/assets/*"
],
"@auth/*": [
"/app/auth/*"
],
"@environments/*": [
"/environments/*"
],
"@store/*": [
"/app/store/*"
],
"@shared/*": [
"/app/shared/*"
],
"@ui/*": [
"/app/ui/*"
],
"@brands/*": [
"/app/brands/*"
],
"@tests/*": [
"/app/tests/*"
]
}
},
"exclude": [
"test.ts",
"**/*.spec.ts"
],
"include": [
"**/*.ts"
]
}
And here is my tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": ".",
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "es5",
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017",
"dom"
],
"types": [
"jest"
],
"paths": {
"@applications/*": [
"./libs/*"
],
"@applications/backend": [
"./libs/backend/src/index.ts"
],
"@applications/env": [
"./libs/env/src/index.ts"
]
}
}
}
Would be great if you have have a solution or workaround.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 16 (2 by maintainers)
@johnRambo2k14
I believe you want app-specific tsconfig path mappings.
However, I am not sure that is possible using
typescriptat this time. I threw together a simple typescript (only typescript, no Nx, no Angular) project to demonstrate it: https://github.com/FrozenPandaz/nx-examples/tree/simplets.It seems like this is not possible using
typescriptbecause"extends"in tsconfigs do a shallow extension… meaning if you have apathsetting intsconfig.child.jsonwhich extendstsconfig.parent.jsonwhich also has apathsetting, thepathsetting fromtsconfig.child.jsonentirely overrides thepathsetting fromtsconfig.parent.json. What you could do (not recommended), is to duplicate thepathmapping for every single app… However, your workspace is most likely going to contain a lot morelibswhich means there’s going to be a lot of duplicatedpathsettings throughout theapps.I am afraid we cannot help with this issue through Nx… Perhaps an issue at https://github.com/Microsoft/TypeScript would be more appropriate.
Late to the party, but it seems that you can set the baseUrl and Paths at the root.
So, while you may not be able to, per project, do this:
It appears that in the root
tsconfig.base.jsonyou can do this:In some ways this is actually better as you can consistently reference dependencies across apps/libs (assuming you’ve set
implicitDependenciesinnx.json).For reference to people having issues with “Module not found” error. Provided tsconfig.app.json’s
baseUrldirects to application source, where it seems that it should direct to workspace root. For me links between modules started working when I either removed thebaseUrlfrom tsconfig.app.json or pointed it to worspace root. Yourtsconfig.json(or in latest nx it’stsconfig.base.json) that lives in workspace root should still have thebaseUrland it seems to be correct.