Reputation: 313
I built a project in node using TS, and I'm trying to implement absolute path for imports.
However, when I run the project it begins failing saying.
[1] Error: Cannot find module 'src/common/logger'
[1] Require stack:
[1] - C:\...xyz\Workspaces\PROJECT\dist\index.js
When I switch 'src/common/logger'
to './common/logger'
it proceeds to point to other ones that absolute path imports.
I figure its obviously something wrong with my TS config, but I can't seem to see where I am messing up.
Ultimately I want the imports to become 'common/xyz'
and 'db/xyz'
My project directory follows this flow:
├───tsconfig.json
├───node_modules
├───dist // output location
└───src
|───common
|───db
└───index.ts
{
"compilerOptions": {
/* Language and Environment */
"target": "es2021",
/* Modules */
"module": "commonjs",
"rootDir": "./src",
"moduleResolution": "node",
"baseUrl": ".",
"paths": {
"src/*": ["./src/*"],
"/*": [ "./src/*" ],
},
"outDir": "./dist"
}
}
My thought is that the issue is how I set up the tsconfig. I tried looking through the documentation, but I clearly am not understanding the paths section. I've tried several changes to the paths, but still can't seem to get it work.
Any help would be greatly appreciated.
Upvotes: 2
Views: 1066
Reputation: 313
Leaving this bread trail for people who come across this issue.
TLDR; There is no way with tsconfig only to do it. You would need to use packages such as Webpack, Gulp, or other npm packages to help resolve the absolute imports.
My advice: If you are working on just getting the project up and running, stick with relative imports for now. Then you can look into the 3rd party tools to implement the absolute pathing.
Upvotes: 7