I am trying to run a Node.js application using TypeScript and Express. However, I am encountering the following issues:
1.When I use the command npm run dev
, I get the error: TypeError: Unknown file extension '.ts'
2.When I try ts-node src/app.ts
, I also receive: TypeError: Unknown file extension '.ts'
3.When I try 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.
My TypeScript configuration (tsconfig.json) is set up with “module”: “ESNext” and “target”: “ESNext”.
My nodemon.json is configured to watch .ts files and use ts-node.
I ensured that reflect-metadata is imported at the top of my app.ts.
Despite this, the application is not working.
My Package.json File:
{
"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"
}
}
My tsconfig File:
"compilerOptions": {
"module": "ESNext",
"target": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"outDir": "./dist",
"rootDir": "./src"
}
My nodemon.json File :
{
"watch": ["src"],
"execMap": {
"ts": "ts-node"
},
"ext": "ts",
"ignore": ["src/**/*.spec.ts"]
}
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?