-1

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?

2
  • Node v20 cannot run typescript natively. You'll need Node v22+ but it's still an experimental feature, so I wouldn't recommend it. ts-node should have worked, what's in your app.js file? Check - nodejs.org/en/learn/typescript/run Commented Dec 24, 2024 at 5:32
  • ts-node src/app.ts should work. Commented Dec 24, 2024 at 6:19

2 Answers 2

1

This is an example of vanilla-typescript project using Vite: Vite Typescript Vanilla in stackblitz

You may follow the configuration it has, and Im sure your project should work. As a recommendation, I would recommend using tsx: https://tsx.is/

$ npm install tsx -D

and then you can use it on a npm script:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "tsx ./app.ts",
    "dev": "tsx watch ./app.ts",
    "build": "tsc"
  },

$ npm run dev

Should work.

Sign up to request clarification or add additional context in comments.

2 Comments

I’m so grateful for your help, the problem is fixed! Thanks a lot!
Could you select my answer as the correct one? @MBHARATH and up-vote
0

How to run TypeScript files from command line?

I think a quick Google search will lead you to this. The TL;DR is that Node.js can understand JavaScript, but not TypeScript.

Use npx ts-node src/foo.ts to convert and run the TypeScript file in one step, or do it in two steps with tsc (the first option is simpler).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.