5

I created a project on the laptop and set it up. Everything worked fine. Now I'm working on PC, I cloned project and when I start server.ts an error occurs:

megaa@DESKTOP-OIQ46IB MINGW64 /d/Code/Pet-project/server (main)
$ npm run start
> [email protected] start
> nodemon --exec ts-node server.ts
[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node server.ts`
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for D:\Code\Pet-project\server\server.ts
 at new NodeError (node:internal/errors:405:5)
 at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:99:9)
 at defaultGetFormat (node:internal/modules/esm/get_format:142:36)
 at defaultLoad (node:internal/modules/esm/load:91:20)
 at nextLoad (node:internal/modules/esm/hooks:733:28)
 at load (D:\Code\Pet-project\server\node_modules\ts-node\dist\child\child-loader.js:19:122)
 at nextLoad (node:internal/modules/esm/hooks:733:28)
 at Hooks.load (node:internal/modules/esm/hooks:377:26)
 at MessagePort.handleMessage (node:internal/modules/esm/worker:168:24)
 at [nodejs.internal.kHybridDispatch] (node:internal/event_target:778:20) {
 code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
[nodemon] app crashed - waiting for file changes before starting...

I tried to solve it with removing "type": "module" from package.json - didn't help as well as starting as nodemon --exec ts-node-esm server.ts or nodemon --exec ts-node --esm server.ts. Moreover I have following rows in my tsconfig.json:

"ts-node": {
 "esm": true,
 "experimentalSpecifierResolution": "node"
}

I installed typescript, tsc, ts-node with -g flag as well as nodemon

tsconfig.json:

{
 "compilerOptions": {
 "target": "ES2021",
 "useDefineForClassFields": true,
 "module": "ESNext",
 "lib": [
 "ES2021",
 "DOM",
 "DOM.Iterable"
 ],
 "skipLibCheck": true,
 /* Bundler mode */
 "moduleResolution": "node",
 "allowImportingTsExtensions": true,
 "allowSyntheticDefaultImports": true,
 "resolveJsonModule": true,
 "esModuleInterop": true,
 "isolatedModules": true,
 "noEmit": true,
 /* Linting */
 "strict": true,
 "noUnusedLocals": true,
 "noUnusedParameters": true,
 "noFallthroughCasesInSwitch": true
 },
 "ts-node": {
 "esm": true,
 "experimentalSpecifierResolution": "node"
 }
}

package.json:

{
 "name": "server-side",
 "version": "1.0.0",
 "description": "",
 "main": "./server.ts",
 "scripts": {
 "start": "nodemon --exec ts-node server.ts"
 },
 "author": "",
 "license": "ISC",
 "dependencies": {
 "@types/express": "^4.17.17",
 "chalk": "^5.2.0",
 "cors": "^2.8.5",
 "express": "^4.18.2",
 "mysql": "^2.18.1",
 "ts-node": "^10.9.1",
 "tsc": "^2.0.4"
 },
 "devDependencies": {
 "@types/cors": "^2.8.13",
 "@types/mysql": "^2.15.21",
 "nodemon": "^2.0.22",
 "typescript": "^5.1.6"
 }
}

Let me know if I need to provide any additional information

asked Jul 9, 2023 at 16:31
3
  • ts-node is a pain to work with. Just compile it with tsc -w and then nodemon dist/index.js or whatever. Nodemon will watch changes in the dist output and restart the program. Commented Jul 9, 2023 at 16:53
  • 1
    Try tsx (tsx watch server.ts) github.com/esbuild-kit/tsx instead, it's more stable with things like this Commented Jul 9, 2023 at 18:43
  • @Dimava it helped. server.ts started. You can write it as an answer and I'll mark it as right Commented Jul 9, 2023 at 19:00

1 Answer 1

14

ts-node and ts-node+nodemon are known to be problematic in certain circuimstances (--inspect, cjs/esm interop, some configs), one of which you have encountered.

Try using a more drop-in-and-it-just-works alternative tsx https://github.com/esbuild-kit/tsx
tsx server.ts

It has a builtin watcher, so nodemon is not needed
tsx watch server.ts


edit: it's 2024 you should try Bun https://bun.sh/ , which is a node/pnpm alternative with great UX. I now use it for all projects which do not explicitly require node, and even for those I use it as package manager

answered Jul 9, 2023 at 19:43
Sign up to request clarification or add additional context in comments.

2 Comments

tsx does not work.
tsx works great! On the other hand, bun has a lot of limitations and is (yet) far from a drop-in alternative to Node.js.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.