TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Project/src/index.ts

114 views Asked by At

I keep getting;TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Project/src/index.ts when I run npm run dev

All my settings are as recommended,

My package.json

{
  "name": "api",
  "version": "0.0.1",
  "description": "",
  "type":"commonjs",
  "scripts": {
    "start": "NODE_ENV=production node build/index.js",
    "dev": "nodemon src/index.ts",
  },
  "dependencies": {
    "@cloudinary/url-gen": "^1.9.1",
    "bcrypt": "^5.0.1",
    "body-parser": "^1.20.0",
    "cloudinary": "^1.34.0",
    "cors": "^2.8.5",
    "date-fns": "^2.29.3",
    "dotenv": "^16.0.1",
    "express": "^4.18.1",
    "express-rate-limit": "^6.7.0",
    "joi": "^17.6.0",
    "jsonwebtoken": "^8.5.1",
    "multer": "^1.4.5-lts.1",
    "nodemailer": "^6.9.1",
    "pg": "^8.4.0",
    "reflect-metadata": "^0.1.13",
    "typeorm": "^0.3.17"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.0",
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "@types/jsonwebtoken": "^8.5.8",
    "@types/node": "^16.11.38",
    "@typescript-eslint/eslint-plugin": "^5.27.0",
    "@typescript-eslint/parser": "^5.27.0",
    "eslint": "^8.16.0",
    "nodemon": "^2.0.16",
    "ts-node": "^10.9.1",
    "typescript": "4.5.2"
  },
  "engines": {
    "node": "18.10.0"
  }
}

My tsconfig.json

{
  "compilerOptions": {
    "lib": ["es5", "es6"],
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "outDir": "./build",
    "rootDir": "./src",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "sourceMap": true,
    "esModuleInterop": true
  },
  "ts-node": {
    "esm": true,
    "experimentalSpecifierResolution": "node"
  },
  "include": ["/**/*.ts"],
  "exclude": ["node_modules"]
}

I dont understand what im doing wrong I tried most of the fixes i found online but most i had already implemented them.

2

There are 2 answers

0
Luís poze On

Nodemon don't understand ts files. Monitor the build folder js files.

{
  "scripts": {
    "start": "NODE_ENV=production node build/index.js",
    "dev": "nodemon src/build/index.js",
  },
}

If you want to run Nodemon on ts file changes use the Typescript compiler (TSC) you have installed. TSC with watch flag, from shell docs.

  --watch, -w  Watch input files.

like so:

{
  "scripts": {
    "start": "NODE_ENV=production node build/index.js",
    "dev": "nodemon src/build/index.js",
    "compile": "tsc -w",
  },
}

Run compile before dev.

Any changes in your ts files will dispatch a build, putting the js files under the configured build folder, and making nodemon act.

0
Franco Mendo On

I suggest alternatives: The npm package "ts-node" is a tool that allows you to run TypeScript code directly in a Node.js environment. This avoids the need to compile TypeScript code to JavaScript before executing it, which can be useful for development and debugging. Or, to enable automatic server reloading, the ts-node-dev command can be used. This command is a version of ts-node that includes the ability to reload the server automatically.