Visual Studio Code can't resolve angular's tsconfig paths
Asked Answered
C

9

52

I'm trying to import some services using barrels and tsconfigs paths options but I can't get angular and vscode to get along.

If it works for one it doesn't for the other and viceversa...

My situation seems to be pretty simple:

  • in src/app/services I have a service which is exported in a index.ts
  • my src/tsconfig.app.json is just this:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "types": [],
    "baseUrl": ".",
    "paths": {
      "services": ["app/services"]
    }
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts"
  ],
}

and my angular app compiles with no issues, but vscode keep giving me errors every time I try to import my service from 'services' giving me [ts] Cannot find module 'services'.

why?

I'm using typescript 3.1.6 and in vscode settings I have "typescript.tsdk": "C:/Users/myuser/AppData/Roaming/npm/node_modules/typescript/lib" (I also tried to leave the default setting, no changes)


edit:

if I specify paths in ./tsconfig.json starting from src, vscode is happy but angular is not.

if I specify paths in both tsconfig.json and src/tsconfig.app.json, both vscode and angular are happy, but it seems a too stupid workaround to me...

Cary answered 6/11, 2018 at 13:52 Comment(2)
And did you try setting the TSDK to your project instead of the NPM one ?Delicatessen
yep, no changes... also, I edited my questionCary
C
58

I figured it out, even if I'm keep thinking that is all absurd...

VsCode automatically looks for a tsconfig.json file and it doesn't care about tsconfig.app.json, so paths needs to be specified in tsconfig.json.

At the same time, the angular-cli scaffolding specify a baseUrl parameter in tsconfig.app.json which overrides the upper one.

The solution is to delete baseUrl parameter in the tsconfig.app.json or edit its value to "../"

(As a personal remark, given that vscode is largely used to build angular solutions, I think that there's something that should be revisited in the angular-cli scaffolding or in how vscode looks for tsconfig files)

Cary answered 6/11, 2018 at 14:23 Comment(6)
I think it's also source of confusion for WebStormAmino
Glad i found this, it would have taken a while before i would have tried that (makes little sense)Straightaway
holy crap, THIS is the answer :) I was getting depressed because it seemed like everyone had solved this problem just by locating paths in tsconfig.app or adding a glob at the end for directories..Pinfold
Also changing the imports to "non-relative" in VS Code setting was a required to step to make this work for meKimbell
if I delete baseUrl or edit to "../" it breaks when I start the app with "npm start" what should I do now?Talyah
This also is strongly actual if you migrated Angular to NX monorepo. Thank you!Avelinaaveline
M
46

this answer worked for me.

  1. Open Command Palette and select TypeScript: select TypeScript Version ... enter image description here

  2. Select to use Workspace Version enter image description here

Hope this answer solves problem of others facing the same problem.

Mulvihill answered 8/5, 2021 at 18:5 Comment(0)
C
10

It seems VSCode only checks the tsconfig.json directly in the folder your open. It does not check folders above like tsc and does not look at tsconfig.app.json. So in some cases the baseUrl might be missing because VSCode simply has not read a tsconfig.json.

VSCode's limitation to a single tsconfig.json in a fixed place has been around for a while and it seems it's not so easy to make this more flexible: https://github.com/microsoft/vscode/issues/12463 (scroll to end)

As mentioned above this is annoying in Angular-CLI scaffolding with multiple sub-projects where there is only one tsconfig.json at the root of the workspace. Obviously the projects might have different baseUrls in their tsconfig.app.json which cannot all go in a single tsconfig.json at the root.

As a workaround I have in addition to the project specific baseUrl in tsconfig.app.json created an extra minimal tsconfig.json for VSCode in the src folder of a sub-project with "baseUrl":"." and open VSCode from there. I have not tested this severely but so far Angular compiling and VSCode intellisense both seem to be happy.

Capone answered 15/9, 2019 at 21:58 Comment(1)
Examples please!Dress
O
9

I was struggling with this issue for a long time and have tried just about every solution on the internet. The project would compile but VSCode would not honor the "paths" declarations in my tsconfig.json.

What finally fixed it for me was removing the "include" and "exclude" sections of my tsconfig.json enter image description here

I'm not sure which one of the 2 was the problem, but removing both finally fixed my long-standing issue.

Oeflein answered 22/3, 2022 at 12:19 Comment(4)
my tsconfig had been working for ages, but decided to go dumb with TS 4.6. i had to actually add src include: [ "src/*", "some other paths" ] to bring it back to life.Psalterium
removing include fixed it for meCila
Seems totally random, but it worked for me on a non-angular project. I couldn't find any way to import from /src in mocha tests (/test) without using ugly relative imports. Also vscode intellisense wouldn't offer completions. I've spent a couple of hours poking around different solutions and finally found this. I giess "do less" is often a good answer!Etruscan
Same happen to me, removing "exclude" section worked... don't see much sense on that BTW... I might be doing something wrong but I don't know what!Abortifacient
D
5

In case you work with project Angular, let place baseUrl and paths in tsconfig.json instead of tsconfig.app.json.

Drypoint answered 25/11, 2019 at 9:4 Comment(0)
W
4
  1. Create tsconfig.json in your application near tsconfig.app.json.
  2. Add next settings
{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "baseUrl": "./",
    "paths": {
       /* your_paths */
    }
  }
}
  1. In your tsconfig.app.json and tsconfig.spec.json rewrite on "extends": "./tsconfig.json"

Final file structure

projects
  your_application
    tsconfig.json
    tsconfig.spec.json
    tsconfig.app.json
tsconfig.json
Woodnote answered 30/3, 2021 at 16:19 Comment(0)
E
3

Another possible reason of issues with resolving aliases can be caused by using include property in main tsconfig file. Default value is ['**/*'] - just add '**/*' to the included paths when you override it. This was my problem, but it was hard to detect that include is causing this.

Eneidaenema answered 4/6, 2020 at 7:12 Comment(0)
N
1

This might be the issue with the editor so go to tsconfig.editor.json and if you don't have tsconfig.editor.json then create one and there extend your tsconfig.app.json, and after that add "references" in your tsconfig.json and add the path of your tsconfig editor.

tsconfig.editor.json => enter image description here

tsconfig.json => enter image description here

Netta answered 21/3, 2023 at 7:19 Comment(0)
B
0

To avoid confusion and duplicate code for other developers you can create tsconfig.json then extend the other tsconfig in my example it was tsconfig.lib.json so my tsconfig.json to be

//!for vscode to detect other tsconfigs paths
{
    "extends": "./tsconfig.lib.json",
}

Note: I had to exit Vscode ( cmd+q in mac ) and reopen it for this to take effect

Banking answered 16/3 at 15:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.