Angular 6 / declare path for library in tsconfig.lib.json
Asked Answered
P

1

7

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.

Pearcy answered 3/9, 2018 at 9:46 Comment(2)
If no one more experienced answers try to tweak baseUrl - maybe the problem lies thereBurcham
angular will use tsconfig file which is configured in angular.json architect --> build --> options --> tsconfig.json. Where VS Code uses tsconfig.json which is in bottom. You need to add paths in boths config to get both work correctly or change project to use base tsconfig.json in angular.json.Cresting
C
8

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.

Cresting answered 4/3, 2019 at 11:38 Comment(2)
Would you paste the paths contents of these files in your solution ?Antipersonnel
Edited my answer to include paths example.Cresting

© 2022 - 2024 — McMap. All rights reserved.