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
Author by
koryakinp
Updated on June 12, 2022Comments
-
koryakinp almost 2 years
The project was generated via angular CLI. I have the following folder structure:
I want to define a path to a bar folder in tsconfig.app.json and import
Car
toGarage
.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 over 6 yearsSorry, it was actually
"paths" : { "@bar" : ["foo/bar"]},
-
Vignesh over 6 yearsedited my code to provide additional details@koryakinp
-
koryakinp over 6 yearsThank 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 over 6 years@koryakinp, sorry, I'm not familiar with VSCode. Consider asking separate question
-
treeno about 6 yearsThe trailing * in the path itself (foo/bar/*) was the crux in my case.