How to run Nodemon with TypeScript in debug mode in WebStorm
Asked Answered
P

1

8

I have a very basic Express server setup with NodeJS/Express + Nodemon. I'm using WebStorm as my IDE.

When running the debug/inspect script, my application is throwing a compile-time error. Following the instructions here, and cognizant of the post here, my (simplified) package.json looks like this:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "dev": "nodemon src/app.ts",
    "debug": "nodemon --inspect=127.0.0.1:9229 src/app.ts"
  },
"devDependencies": {
    "@types/express": "^4.17.9",
    "@types/node": "^14.14.19",
    "nodemon": "^2.0.6",
    "ts-node": "^9.1.1",
    "typescript": "^4.1.3"
  }

When I run npm run debug I receive the following error:

enter image description here

I've also tried the following, all with the same result:

  • "debug": "nodemon --inspect src/app.ts"
  • "debug": "nodemon --inspect=0.0.0.0:9229 src/app.ts"

Any ideas why this error is occurring?

The command npm run dev does not generate an error. Upon further investigation, nodemon automatically substitutes ts-node when targeting a .ts file. Ts-node, in turn, recommends debugging by registering ts-node and running the server with the node command.

If you need to use advanced node.js CLI arguments (e.g. --inspect), use them with node -r ts-node/register instead of the ts-node CLI.

Priestcraft answered 4/1, 2021 at 1:6 Comment(0)
T
26

You can modify your script as follows:

"debug": "nodemon --exec \"node --inspect-brk=0.0.0.0:9229 --require ts-node/register src/app.ts\""

to make sure that the --inspect-brk is passed to Node.js and not to ts-node

Tegument answered 4/1, 2021 at 12:5 Comment(5)
Thanks! I hope you guys can put this into your core documentation!Priestcraft
I don't fully understand the --exec, could you also put this in a nodemon.json file? It would be nice if you could put some more text into it to provide more info about what is going on here.Bruxelles
just coming here to say thanksDaffie
For me this works only with: --inspect= instead of: --inspect-brk=Sway
I had to manually install ts-node, but then this worked. npm i -D ts-nodeTroika

© 2022 - 2024 — McMap. All rights reserved.