0

I am able to run my Express app from IntelliJ by creating a debug configuration.

enter image description here

However, when I place breakpoints and post a request to my app from Postman, the app does not stop at my breakpoints, although the application does execute and handle the request.

Do I need to do something special to debug my application?

ab11
  • 19,770
  • 42
  • 120
  • 207

1 Answers1

1

You have to modify your Npm script to make sure that Node.js is started with appropriate debug options (--debug-brk, --inspect-brk, etc) by passing $NODE_DEBUG_OPTION to it , as the IDE can't control the way child processes are spawned - it can only pass options to the main process when starting it. If dev is the npm script that starts the app you'd like to debug with node.js, you need to modify this script accordingly, like:

"dev": "node $NODE_DEBUG_OPTION bin/www" 

These articles might be helpful: IntelliJ IDEA how to correctly pass $NODE_DEBUG_OPTION to npm-run-all and http://pavelpolyakov.com/2016/05/01/webstorm-npm-tasks-debug/.

lena
  • 90,154
  • 11
  • 145
  • 150
  • thanks. this seems right, but i don't think i did it correctly. i modified the `scripts` tag in my package.json. previously i had `"dev": "nodemon --exec 'nf start'"`, which I changed to `"dev": "nodemon $NODE_DEBUG_OPTION --exec 'nf start'"`. I intended to add `--debug-brk` to the `node parameters` in the debug configuration, but when I run the configuration I get this output (see next comment), even though my configuration does not include any `node paramters` (the app behaves as it does previously if I revert my package.json changes) – ab11 Jan 28 '19 at 17:25
  • `[nodemon] 1.18.9` `[nodemon] to restart at any time, enter 'rs'` `[nodemon] watching: src/**/*` `[nodemon] starting nf start --inspect-brk=56003` `error: unknown option --inspect-brk` `[nodemon] app crashed - waiting for file changes before starting...` – ab11 Jan 28 '19 at 17:26
  • i get a similar issue even if I hard code that flag. `"dev": "nodemon --inspect --exec 'nf start'"`, gives `error: unknown option '--inspect'` – ab11 Jan 28 '19 at 17:32
  • it's strange, as `nodemon` does support these options. what nodemon version do you use? – lena Jan 28 '19 at 18:14
  • as far as I can see, when starting nodemon like `nodemon --inspect --exec "node src/main.js"`, the `--inspect` is actually appended to the command line, so that this option is passed to application being run, like `node src/main.js --inspect`. This can explain the error – lena Jan 28 '19 at 18:36
  • I think this syntax is correct, as my application runs without error, but IntelliJ still doesn't stop on my breakpoints: `"dev": "nodemon --exec 'nf start'"`, `"start": "node --inspect forever",` – ab11 Jan 28 '19 at 19:09
  • try changing `"start"` to `"start": "node $NODE_DEBUG_OPTION forever"`. Not sure what is `nf` and `forever`, but, as i wrote, you need to make sure to pass `$NODE_DEBUG_OPTION` to node that starts your application – lena Jan 28 '19 at 19:22
  • i'm not able to get `$NODE_DEBUG_OPTION` to pass, but i'm pretty sure this is correct because it seems to respect the debug port I specify (the app logs the debug port when it starts, and the port changes when I modify the command, however intellij simply does not stop on the breakpoints – ab11 Jan 28 '19 at 19:50
  • "dev": "nodemon --exec 'nf start'", "start": "node --inspect=9229 forever", – ab11 Jan 28 '19 at 19:50
  • 1
    when debugging NPM in WebStorm, you have to use `$NODE_DEBUG_OPTION`, replacing it with `--inspect`, etc. won't work – lena Jan 29 '19 at 10:31