I'm trying to configure the paths for an Angular 6
library (I have successfully configured the paths for a previous Angular project)
Here is what is working for my previous app in the tsconfig.json
file:
"compilerOptions": {
[...]
"baseUrl": "src",
"paths": {
"@class/*": [ "app/class/*" ],
"@services/*": [ "app/services/*" ],
"@views/*": [ "app/views/*" ]
}
}
and then use it like for example:
Import { Item } from '@class/item';
In my new app I'm trying the same way in the tsconfig.**lib**.json
file:
"compilerOptions": {
[...]
"baseUrl": "src",
"paths": {
"@class/*": [ "lib/class/*" ],
"@services/*": [ "lib/services/*" ],
"@views/*": [ "lib/views/*" ]
}
}
I have tried to import a class in a component for my library like this, but it does not work (VSCode cannot find the file):
Import { Item } from '@class/item';
Note that the import
statement in the main project is working:
Import { Item } from 'myLibrary';
Any idea of what I'm not doing well ?
Thanks for your help.
This is from my above comment, if you see that this is a correct answer.
Angular will use the tsconfig.json
file, which is configured in the angular.json
file in **architect --> build --> options --> tsconfig**
. Whereas VS Code
uses the tsconfig.json
file, which is at the bottom of the workspace. You need to add the paths in both configurations to get both of them to work correctly, or change the project to use the base tsconfig.json
file in the angular.json
file.
The tsconfig.json
file has a property that extends which will take another file as base and override everything which are declared in the tsconfig.lib.json
file. So if you have paths declared in the tsconfig.lib.json
file, then the paths are no longer needed in the tsconfig.json
file.
EDIT 29.7.2019 tsconfig.json
"baseUrl": "src",
"paths": {
"@ProjectName1/*": ["../projects/ProjectName1/src/app/modules/*"],
"@Projectname2/*": ["../projects/Projectname2/src/app/modules/*"],
"@environment/*": ["environments/*"],
"project-core": ["../dist/project/project-core"],
"project-core/*": ["../dist/project/project-core/*"]
}
And currenlty I dont't have paths declared in tsconfig.app.json because it would be same as in tsconfig.json. So this is different from my earlier answer, because I have paths only in tsconfig.json.