I am trying to setup aliases for my mock server. Whenever I try to compile ts
files, it returns error that it couldn't find proper modules even though those are defined in tsconfig,json
->paths
Folder structure:
├── server
│ └── src
│ └──/json
├── src
│ └──/modules
├── tsconfig.json
Here is my tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src",
"experimentalDecorators": true,
"jsx": "react",
"lib": [
"dom",
"es2015",
"es2015.promise"
],
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"noUnusedLocals": true,
"esModuleInterop": true,
"paths": {
"@project/app/modules/*": [
"modules/*"
],
"@project/server/data/*": [
"../server/src/json/*"
]
},
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"tools"
]
}
Error:
Error: Cannot find module '@project/server/data/accounts/accountsList'
I faced the same issue. I tried many things and now i got a solution which works for me. I have an app and a library in my angular project. I want to use a library alias in my app.
I have following structure:
├── projects
│ └── lib
│ └── src
│ └── lib
│ └── lib-service.ts
│ └── index.ts
│ └── app
│ └──tsconfig.app.json
├── tsconfig.json
In the tsconfig.json file in the root folder I have following paths defined:
"paths": {
"my-lib": [
"projects/lib/src/index.ts"
]
}
There I access an index.ts file where I define the things I want to export. In my case the index.ts file has the following entry:
export * from './lib/lib-service';
Now I can access the LibService in components of the app with the help of the alias:
import {LibService} from 'my-lib';
It is important to mention that this soloution don't work if I add this entry to the tsconfig.app.json file. I think the IDE (in my case WebStorm) searches for aliases in the tsconfig.json file which is close to the root folder. So I extend the tsconfig.json in my tsconfig.app.json
"extends": "../../tsconfig",
Maybe you have to restart you IDE to remove the red underline of the import line.
I hope this solution works for you. You have to to a little bit more of setup work because you have to define the classes you want to export in the index.ts file. But in case of using libraries it make sense because you don't want to make everything visible for other app.