'NodeJs typescript and module issues
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.
Solution 1:[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.
Solution 2:[2]
This can come from multiple points (comment if I forgot something since this is a somewhat generic config error):
- Change your main to ts-node (as suggested by a different answer) or change the
mainfield in yourpackage.jsonto<buildFolder (normally diet)>/server.jsto reference the built file rather than the typescript file. - Change the
modulefieldtsconfig.jsontoCommonJSto avoid building a ES6 module (this is a problem especially with webpack
Solution 3:[3]
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
Solution 4:[4]
{
"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"
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user7605594 |
| Solution 2 | Elias Schablowski |
| Solution 3 | decoder |
| Solution 4 |
