Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define path in tsconfig.app.json for angular project

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

like image 341
koryakinp Avatar asked Oct 09 '17 04:10

koryakinp


3 Answers

You need to define paths like that:

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

For more information read

  • Configuring TypeScript compiler
like image 179
Max Koretskyi Avatar answered Nov 01 '22 01:11

Max Koretskyi


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": {
  ...
}
like image 40
Philip John Avatar answered Nov 01 '22 01:11

Philip John


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

like image 1
Vignesh Avatar answered Nov 01 '22 01:11

Vignesh