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

3
  • you will find this article helpful, it explains how and why use paths quite elaborately Commented Oct 9, 2017 at 5: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 ? Commented Oct 9, 2017 at 6:17
  • I've posted my answer Commented Oct 9, 2017 at 6:31

4 Answers 4

6

You need to define paths like that:

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

For more information read

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you sir, it solved the original problem, however the editor (in my case VS code) still complains '[ts] Cannot find module '@bar/Car'.'
@koryakinp, sorry, I'm not familiar with VSCode. Consider asking separate question
The trailing * in the path itself (foo/bar/*) was the crux in my case.
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": {
  ...
}

Comments

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

2 Comments

Sorry, it was actually "paths" : { "@bar" : ["foo/bar"]},
edited my code to provide additional details@koryakinp
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.