node.js – Unable to run compiled TypeScript code due to “Cannot find module” error
Here’s my tsconfig.json configuration:
{
"compilerOptions": {
"target": "es2016",
"lib": ["es6"],
"module": "commonjs",
"rootDir": "./",
"baseUrl": "./",
"resolveJsonModule": true,
"allowJs": true,
"outDir": "build",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": false,
"noImplicitAny": false,
"skipLibCheck": true
},
"ts-node": {
"swc": true
}
}
I’m using relative imports in the code with the help of baseUrl
.
The code compiles without any issues using tsc -p .
, but when I try to run it with NODE_PATH=./build node ./build/app.js
, I encounter the following error:
Error: Cannot find module '<PROJECT_FOLDER_NAME>/app/listeners/module.ts'
Require stack:
{
code: 'MODULE_NOT_FOUND',
requireStack: [
'<PROJECT_FOLDER_NAME>/bot/build/app/listeners.js',
'<PROJECT_FOLDER_NAME>/bot/build/app.js'
]
}
In the listeners.js, a loop reads all the files under the app/listeners
folder and calls the default exported function in the module.
I don’t know if it’s the expected behavior but looks like it searches the TypeScript module under app
instead of looking for the JS version under build/app
.
How can I resolve this issue and successfully run my compiled TypeScript code?
Read more here: Source link