2

I have a test nodeJS server code in typescript. I get this error when trying to run the ts file:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for

My package.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "server",
  "main": "server.ts",
  "type": "module",
  "scripts": {
    "test": "test server",
    "prebuild": "tslint -c tslint.json -p tsconfig.json --fix",
    "build": "tsc",
    "prestart": "npm run build",
    "start": "node .",
    "tsc": "tsc"
  },
  "author": "HL",
  "license": "ISC",
  "devDependencies": {
    "@types/node": "^14.0.6",
    "tslint": "^6.1.2",
    "typescript": "^3.9.5"
  },
  "dependencies": {
    "@types/express": "^4.17.6",
    "ejs": "^3.1.3",
    "express": "^4.17.1",
    "http": "0.0.1-security",
    "mysql": "^2.18.1",
    "util": "^0.12.3"
  }
}

Note I need to use type: module, because I get an error when I remove it:

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1067:16)
    at Module._compile (internal/modules/cjs/loader.js:1115:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1171:10)
    at Module.load (internal/modules/cjs/loader.js:1000:32)
    at Function.Module._load (internal/modules/cjs/loader.js:899:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47

Please help.

Hagay Levy
  • 61
  • 1
  • 2
  • 5

4 Answers4

1

You need to compile before running with node, or use ts-node to run your typescript. Also, this may be an issue stemming from your tsconfig and imports.

1

This can come from multiple points (comment if I forgot something since this is a somewhat generic config error):

  1. Change your main to ts-node (as suggested by a different answer) or change the main field in your package.json to <buildFolder (normally diet)>/server.js to reference the built file rather than the typescript file.
  2. Change the module field tsconfig.json to CommonJS to avoid building a ES6 module (this is a problem especially with webpack
Elias Schablowski
  • 2,619
  • 9
  • 19
0

After many tries the only way i got my setup to work is as follows:

  • Remove "type": "module" from package.json
  • Set "module": "commonjs" inside tsconfig.json
  • Run your server entry with ts-node ./src/app.ts
Ali H. Kudeir
  • 756
  • 2
  • 9
  • 19
-1
{
  "name": "project",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "build": "tsc",
    "start": "tsc && node ./build/index.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/cors": "^2.8.12",
    "@types/express": "^4.17.13",
    "bcrypt": "^5.0.1",
    "cors": "^2.8.5",
    "dotenv": "^16.0.0",
    "express": "^4.17.2",
    "moment": "^2.29.1",
    "pg": "^8.7.3",
    "pg-hstore": "^2.3.4",
    "sequelize": "^6.15.1"
  },
  "devDependencies": {
    "@types/bcrypt": "^5.0.0",
    "@types/moment": "^2.13.0",
    "@types/typescript": "^2.0.0",
    "nodemon": "^2.0.15",
    "typescript": "^4.5.5"
  }
}
  • You need to format your code and give instruction details. – Nick Vu Mar 10 '22 at 09:19
  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Mar 10 '22 at 09:19