I am trying to run a Node.js application using TypeScript and Express. However, I am encountering the following issues:
When I run npm run dev
, I get: TypeError: Unknown file extension '.ts'
When I run ts-node src/app.ts
, I get: TypeError: Unknown file extension '.ts'
When I run ts-node dist/app.js
, I get: Error: Cannot find module
What I Have Tried:
I have installed all necessary dependencies as listed in my package.json
.
Here’s my tsconfig
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./dist",
"rootDir": "./src"
}
Here’s my nodemon
config,
{
"watch": ["src"],
"execMap": {
"ts": "ts-node"
},
"ext": "ts",
"ignore": ["src/**/*.spec.ts"]
}
Here’s my package.json
,
{
"name": "Testing",
"version": "1.0.0",
"description": "",
"main": "src/app.ts",
"type": "module",
"scripts": {
"test": "echo "Error: no test specified" && exit 1",
"start": "node dist/app.ts",
"dev": "nodemon",
"build": "tsc"
},
"dependencies": {
"dotenv": "^16.4.5",
"express": "^4.19.2",
"npm": "^11.0.0",
"reflect-metadata": "^0.1.13",
"routing-controllers": "^0.10.4",
"typeorm": "^0.3.20"
},
"devDependencies": {
"@types/express": "^4.17.21",
"@types/node": "^20.17.10",
"@types/request": "^2.48.12",
"nodemon": "^3.1.9",
"ts-node": "^10.9.2",
"typescript": "^5.7.2"
}
}
Environment:
Node.js version: 20.12.2
npm version: 6.14.11
How can I resolve the errors I am encountering and run my TypeScript and Express application correctly? Are there any issues with my configuration, or am I missing something critical?