skip to Main Content

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?

3

Answers


  1. Try ‘deno’ instead of node, it has build in native typescript support, created by Ryan Dahl, the man who also created Node.js.
    However, it is not widely adapted even after 5 years on the run. Node still rules.

    Login or Signup to reply.
  2. 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).

    Login or Signup to reply.
  3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search