define path in tsconfig.app.json for angular project
Asked Answered
D

4

6

The project was generated via angular CLI. I have the following folder structure:

enter image description here

I want to define a path to a bar folder in tsconfig.app.json and import Car to Garage.

My tsconfig.app.json:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    ...
    "baseUrl": "./",
    "paths" : {
      "@bar" : ["foo/bar"]
    },
    ...
  },
  ...
}

My Garage.ts:

import { Car } from "@bar/Car";
export class Garage {}

In garage.ts I have an error:

Cannot find module '@bar/car'.

Douglas answered 9/10, 2017 at 4:31 Comment(3)
you will find this article helpful, it explains how and why use paths quite elaboratelyBilestone
@AngularInDepth.com, Thank you sir. I went through the article briefly, but still don't get it. Can you please give some insights on why the Car module can not be resolved in that particular example ?Douglas
I've posted my answerBilestone
B
6

You need to define paths like that:

   "paths" : {
      "@bar/*" : [
          "foo/bar/*"
      ]
    },

For more information read

Bilestone answered 9/10, 2017 at 6:31 Comment(3)
Thank you sir, it solved the original problem, however the editor (in my case VS code) still complains '[ts] Cannot find module '@bar/Car'.'Douglas
@koryakinp, sorry, I'm not familiar with VSCode. Consider asking separate questionBilestone
The trailing * in the path itself (foo/bar/*) was the crux in my case.Meaning
O
5

Please do not forget to put the baseUrl first before adding the paths. I spent hours trying to figure out where I was wrong.

"baseUrl": "./",
"paths": {
  ...
}
Othella answered 5/10, 2018 at 18:42 Comment(0)
K
1

I think this might work

    {
  "extends": "../tsconfig.json",
  "compilerOptions": {
    ...
    "baseUrl": "./",
    "paths" : {
      "@bar" : ["foo/bar"],
      "@environment/*": ["environments/*"],
        "@shared/*": ["app/_shared/*"],
        "@helpers/*": ["helpers/*"]
       //you can define multiple paths inside this 
    },
    ...
  },
  ...
}

and the question looks like duplicate of question

Killerdiller answered 9/10, 2017 at 4:44 Comment(2)
Sorry, it was actually "paths" : { "@bar" : ["foo/bar"]},Douglas
edited my code to provide additional details@DouglasKillerdiller
W
0

to stop the complain in VS code : CMD + SHIFT + P then type reload or Reload Project to stop the complain in webstorm : invalidating caches (File | Invalidate caches, Invalidate and restart)

Wedgwood answered 6/2, 2021 at 22:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.