Search code examples
angulartypescriptimportangular-clitsconfig

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


Solution

  • You need to define paths like that:

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

    For more information read