I have a TypeScript project with a specific directory structure and a custom tsconfig.json. After successfully compiling the TypeScript code using the tsc command, I encounter an issue when trying to run the compiled JavaScript code using node dist/app.js. The error suggests a problem with module resolution, specifically with the import statement in my app.ts file:
import { environment, logger } from "~/configs";
The error message I receive is:
Error: Cannot find module '~/configs'
Require stack:
- /home/kiran/xxx/app/dist/src/app.js
I'm also facing similar issues when using ts-node-dev for development, with the error pointing to the same module resolution problem.
Here are the relevant details:
Directory structure:
. ├── src │ ├── app.ts │ ├── configs │ └── controllers ├── tsconfig.json └── yarn.locktsconfig.json:{ "compilerOptions": { "allowJs": false, "strict": true, "declaration": true, "types": ["node"], "lib": ["ES2017", "ES6", "ES7", "DOM"], "esModuleInterop": true, "module": "commonjs", "moduleResolution": "node", "resolveJsonModule": true, "isolatedModules": true, "target": "ESNext", "experimentalDecorators": true, "emitDecoratorMetadata": true, "skipLibCheck": true, "outDir": "dist", "baseUrl": "src", "paths": { "~/controllers": ["controllers"], "~/configs": ["configs"], }, }, "exclude": ["node_modules", "dist"] }Versions:
- TypeScript: 5.3.3
- Node: 18.18.0 (also tested on 20.x.x)
How to resolve this module resolution issue and successfully run my compiled TypeScript code in Node.js? Additionally, I've provided the commands I use for development with ts-node-dev, which also result in a similar error.
Tried:
- to change
~with# - to use globs like
~/*intsconfig - to downgrade TypeScript to older versions