tsconfig.json
{
"compilerOptions": {
"baseUrl": "./",
"allowJs": false,
"strict": true,
"target": "es6",
"module": "commonjs",
"lib": ["dom", "es6", "es5", "es2017", "esnext.asynciterable"],
"sourceMap": true,
"moduleResolution": "node",
"paths": {
"src/*": ["src/*"]
},
"declaration": true,
"rootDir": "./src",
"outDir": "dist"
},
"exclude": ["dist"],
"include": [ "src"]
}
Original Folder structure
src /
util/
getSomething.ts
index.ts
Output Folder structure
dist /
util/
getSomething.js
index.js
index.ts
import getSomething from 'src/util/getSomething';
package.json script
{
"build": "tsc && tspath -f",
"start": "node dist/index.js"
}
The build works but when I run start I get error. Is there a way to have a root based module resolution? I followed this example (just replacing ~ with src) and it is giving me this problem. Unless there is an alternative solution to tspath. https://haseebmajid.dev/blog/better-imports-with-babel-tspath
Note: I am using windows 10.
npm start
Error: Cannot find module './src\util\getSomething' Require stack: c:\apps\example-typescript-node\dist\index.js
Answered here: https://github.com/duffman/tspath/issues/33
I have resolved my problem. I had to ensure my rootDir was ./ not src. Then targeting index.js inside dist/src/ not dist/
tsconfig.json
{
"compilerOptions": {
"rootDir": "./"
}
}
package.json
{
"build": "tsc && tspath -f",
"start": "node dist/src/index.js"
}