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'.

koryakinp
  • 3,989
  • 6
  • 26
  • 56
  • you will find [this article](https://blog.angularindepth.com/configuring-typescript-compiler-a84ed8f87e3#06ea) helpful, it explains how and why use `paths` quite elaborately – Max Koretskyi Oct 09 '17 at 05:48
  • @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 ? – koryakinp Oct 09 '17 at 06:17
  • I've posted [my answer](https://stackoverflow.com/a/46640069/2545680) – Max Koretskyi Oct 09 '17 at 06:31

4 Answers4

6

You need to define paths like that:

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

For more information read

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
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": {
  ...
}
Philip John
  • 5,275
  • 10
  • 43
  • 68
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

Vignesh
  • 1,140
  • 1
  • 9
  • 17
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)