How to debug a node app from WebStorm? - node.js

I am running node --inspect app.js inside of a docker container.
In the console I see Debugger listening on ws://127.0.0.1:9229/0b4d64ac-90c1-480d-a98a-7061c0b49189
How can I debug using WebStorm? I know on a previous version of node there was --debug I would then connect using the Node.js Remote Debugger.

To attach to remote Node.js app started with --inspect/--inspect-brk, (Node.js 7+), you need using Chromium Remote run configuration.
See also https://blog.jetbrains.com/webstorm/2017/09/debugging-node-js-apps-in-webstorm/, https://www.jetbrains.com/help/webstorm/running-and-debugging-node-js.html#node_docker_run_debug

Related

Chrome does not detect debugger if attached after process start

When I start an Express server with the --inspect option, I can navigate to chrome://inspect/#devices and launch a debug console for the appropriate Remote Target. But, when I start the process without the --inspect option, then try to initiate the inspector with kill -10 $PID, Chrome does not detect the Remote Target, even though the server indicates that the debugger was started. Is there anything I can do to connect to this process by Chrome or some other means?
This is the output from the server after running kill -10 $PID:
Attaching to nodejs
nodejs | Running version 3 on http://0.0.0.0:5000
nodejs | Debugger listening on ws://127.0.0.1:9229/c948d085-0491-4cd9-9832-a0f0cf120e11
nodejs | For help, see: https://nodejs.org/en/docs/inspector
Another note: I am running this app in a container with a Docker bridge network, but I don't see why that should make a difference because the --inspect option registers the same output when I start the server with it.
Versions:
Docker version 19.03.13, build 4484c46d9d
Node v12.22.1
express#4.17.1
Additional note: the procedure I'm describing (starting debug session after server start) is described here: nodejs.org/en/docs/guides/debugging-getting-started
But, when I start the process without the --inspect option, Chrome does not detect the Remote Target
If you started a process without --inspect it is expected that you won't be able to debug your code. Make sure that that logs you are talking about are the latest and some junk from past runs before you removed --inspect flag

WebStorm: node.js debugger vs remote node.js

I've encountered two debugging configurations for node.js in WebStorm:
Node.JS debug and node.js Remote Debug. You can see both of them here:
My question is what is the difference between these two? and when should we use each one of them?
With Remote Debug configuration, you can attach the debugger to already running Node application started with --debug-brk. See https://www.jetbrains.com/help/webstorm/2016.3/running-and-debugging-node-js.html#remote_debugging, https://confluence.jetbrains.com/display/WI/Running+and+debugging+Node.js+application#RunninganddebuggingNode.jsapplication-DebuggingNode.jsappthatrunsremotely
With Node.js run configuration, you can start the Node.js interpreter (either local or remote) and pass your application to it.

What's the best way to debug a NodeJs app running on a docker container in a remote host?

I have a NodeJs app running on a docker container on a remote server. I can access the app on the browser. I'm also able to deploy to my app using PhpStorm and its remote server connection.
However, I tried to use the remote NodeJs debug tool of PhpStorm and it doesn't work. I always get connection refused.
I know the debug port is open because I check the docker containers and the 5858 is open. This port is also oppened on the host. And this is also the port I set for the debug.
package.json:
"scripts": {
"start": "nodemon --debug=5858 index.js myApp"
}
I don't know if PhpStorm is the best solution to debug this kind of app. So if someone has a better idea please let me know.
Thanks!
After further searching I found this great repository:
https://github.com/seelio/node-inspector-docker
It seems to me the easier way to make the app running and debug it.
Definitely node-inspector,
I had to do the same for an app in microservices and clusters/workers
just in case you need it: clustered apps with node-inspector
You can use intelij IDEA as IDE
It support running app directly from docker and allows you to debug apps easily.
once configured with your docker image its done.
next time just click run and it will start quickly nodejs inside your docker and show logs etc all just like we do with local node instance
https://www.jetbrains.com/help/idea/2016.3/running-and-debugging-node-js.html#node_docker_run_debug
Its EAP and communitiy editions are always is free

Unable to start the nodeapp through terminal

I have a node app locally and I normally run it using the run button in the web storm, which works fine. While I am documenting the project I came across this blocker: if I try running the same app through the terminal using the command: node app.js , the server starts but the browser throws and error stating "This site can’t be reached" "localhost refused to connect".
Because the port is occupied.
You can change it using app.listen({port})...
or
linux:
export PORT=4500
node server.js
windows:
set PORT=4500
node server.js
I found this myself.. since I used the scaffolding app and it stores info related to server at bin/www, so we will not be able to run the app using the command : node app.js but instead we could run the app using nodemon(which I installed globally on my machine)
Love nodemon for this awesome feature <3 :)

How to debug two node applications at same time using node's built-in debugger?

I debug my node applications by executing node debug app in my application folder.However if i want to use the same with other application at same time it shows me error.
How can i use debug with the other application too?
Node's default debug port is being used by the first process. Start your second app specifying another port using the --debug-brk or the --debug flag like this:
node --debug-brk=5859 app.js
Then open another terminal window and connect to the second process's debugging port using:
node debug localhost:5859

Resources