define path in tsconfig.app.json for angular project

14,476

Solution 1

You need to define paths like that:

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

For more information read

Solution 2

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": {
  ...
}

Solution 3

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

Share:
14,476
koryakinp
Author by

koryakinp

Updated on June 12, 2022

Comments

  • koryakinp
    koryakinp almost 2 years

    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
    koryakinp over 6 years
    Sorry, it was actually "paths" : { "@bar" : ["foo/bar"]},
  • Vignesh
    Vignesh over 6 years
    edited my code to provide additional details@koryakinp
  • koryakinp
    koryakinp over 6 years
    Thank you sir, it solved the original problem, however the editor (in my case VS code) still complains '[ts] Cannot find module '@bar/Car'.'
  • Max Koretskyi
    Max Koretskyi over 6 years
    @koryakinp, sorry, I'm not familiar with VSCode. Consider asking separate question
  • treeno
    treeno about 6 years
    The trailing * in the path itself (foo/bar/*) was the crux in my case.