0

I'm running one node.js application with "npm run dev" without problems and there are some typescript scripts/files. I'm using nodemon to run my code:

this is my package.json:

{
  "scripts": {
    "start": "ts-node ./src/index.ts",
    "dev": "nodemon --watch \"./src/**/*.ts\" --exec \"ts-node\" ./src/index.ts"
  },
  "devDependencies": {
    "@types/express": "^4.17.2",
    "@types/socket.io": "^2.1.4",
    "nodemon": "^2.0.22",
    "ts-node": "^8.10.2",
    "typescript": "^3.7.2"
  },
  "dependencies": {
    "express": "^4.17.1",
    "socket.io": "^2.3.0"
  }
}

And this is my file structure:

enter image description here

I want show the console.log messages in the console that I have written in the server.ts file but I'm unable to show it.

I tried using --inspect flag in the dev key:

"dev": "nodemon --watch \"./src/**/*.ts\" --exec \"ts-node\" ./src/index.ts"

so being that way:

"dev": "nodemon --inspect --watch \"./src/**/*.ts\" --exec \"ts-node\" ./src/index.ts"

however I got one error:

Unknown or unexpected option: --inspect

I also tested:

--inspect-brk

And I got Unknown or unexpected option: --inspect-brk

As we can see:

enter image description here

no message is showed in the console from the typescript files. I would like to know how I can fix this problem by using nodemon or avoiding it. thanks so much.

1 Answers1

0

nodemon + ts-node doesn't support --inspect
To support it I had to do very long lines, using nodemon to just launch node

"dev": "nodemon --watch './src/**/*.ts' --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm/transpile-only --inspect ./src/index.ts'",

I highly recommend switching to tsx (https://github.com/esbuild-kit/tsx), then it's just

"dev": "tsx watch --inspect ./src/index.ts"

with compile caching and better cjs/esm interop


Another option is to run it in the VSCode Javascript Debugging Terminal which patches node to always run with --inspect on

Dimava
  • 7,654
  • 1
  • 9
  • 24
  • Thanks I understand the first part that nodemon and ts-node doesn't support --inspect. What I don't understand is how I can copy this: nodemon --watch src --exec 'node --experimental-specifier-resolution=node --loader ts-node/esm/transpile-only --inspect src/index.ts' to my "dev": "nodemon --watch \"./src/**/*.ts\" --exec \"ts-node\" ./src/index.ts" Could you add it and change your reply. thanks – FreddicMatters Apr 29 '23 at 21:41
  • You can't `ts-node --inspect`, you have to `node --inspect -r ts-node/register`. See https://stackoverflow.com/questions/49042830/why-does-the-node-inspector-not-start-when-i-am-using-nodemon-and-ts-node . – Dimava Apr 29 '23 at 22:05
  • You could not need `--experimental-specifier-resolution=node`, I needed it for es modules in node16 – Dimava Apr 29 '23 at 22:09
  • Thanks, I was able to run `"dev": "nodemon --watch \"./src/**/*.ts\" --exec node --inspect-brk -r ts-node/register \"ts-node\" ./src/index.ts"` see here: https://imgur.com/a/w9RfSB6 but my server doesn't start I can't access to localhost:5000 – FreddicMatters Apr 29 '23 at 22:16
  • @FreddicMatters you need `"dev": "nodemon --watch \"./src/**/*.ts\" --exec node --inspect -r ts-node/register ./src/index.ts "` – Dimava Apr 30 '23 at 00:16
  • @FreddicMatters seriously, try `tsx` if possible – Dimava Apr 30 '23 at 00:19
  • @FreddicMatters also consider updating your TS, it's already typescript@5.0.4 – Dimava Apr 30 '23 at 00:20
  • thanks I did it and now worked (https://imgur.com/a/lVZgXBr), the server is running, however I can't see any console.log from the file server.ts Also I can see in the console of google chrome of chrome://inspect i t says server running but also on there there isn't any log. Do you have any idea about this why doesn't work? It seems that something is locking the logs. My last option was put server.ts in this: `"dev": "nodemon --watch \"./src/**/*.ts\" --exec node --inspect -r ts-node/register ./src/index.ts .src/server.ts"` but not logs. thanks – FreddicMatters Apr 30 '23 at 02:41