How to debug a basic node.js application (not http) on windows - node.js

I know how to debug http applications using node-inspector and iisnode. But can I use node-inspector to debug a non http node application, on windows?
I tried:
node debug test.js
It says:
debugger listening on port 5858
But opening http://localhost:5858/ in Chrome does not do anything.
BTW: running node debug test.js does start the command-line debugger which works. But it's nothing like node-inspector.

To use node-inspector, the right switch is node --debug not node debug
Here are the detailed steps:
install node-inspector globally (npm install -g node-inspector)
from a command-line window, run: node-inspector
open Chrome and go to http://localhost:8080/debug?port=5858. You'll get the node-inspector UI but without any running app.
from another command-line window, run your app with the --debug switch like this: node --debug test.js
refresh the Chrome tab and voila!
A few interesting points:
If you kill your app and start it again, just refresh the node-inspector tab. It will keep all your breakpoints.
To break automatically on the first line start your app with node --debug-brk test.js

Some links which might help you:
http://vimeo.com/19465332 (screencast from Ryan himself).
https://github.com/joyent/node/wiki/using-eclipse-as-node-applications-debugger

It says: debugger listening on port 5858
I wondered myself about this but since the Node.js documentation indicates that the debugger is accessible via a simple TCP protocol and says nothing about HTTP my guess is that no, it won't be available at _http://localhost:5858.
"V8 comes with an extensive debugger which is accessible out-of-process via a simple TCP protocol" - http://nodejs.org/api/debugger.html

Very recently Microsoft released the node.js tools for Visual Studio. It has the very comfortable Visual Studio debugging for node.js.

node-inspector could be very helpful.
Use it from any browser supporting websockets.
Breakpoints, profiler, livecoding, etc..
http://erickrdch.com/2012/09/debug-a-nodejs-app-with-chrome-dev-tools.html

FYI, in OSX 10.8, Chrome v26 doesn't seem to work, but Safari 6 does using the same instructions as above and using 0.0.0.0:8080 to conect.
There is another post by Danny Coates somewhere that says to do it in the following order:
Your node process: node --debug (or --debug-brk) my_program.js
Node-inspector: node-inspector
The browser pointed to 0.0.0.0:8080

If you are a noob like me on Windows, and you get 'node-inspector not recognized' or something about windows JScript error... despite global install, adding to PATH, etc. then this may help.
Navigate to C:\Users\urusername\AppData\Roaming\npm
Then run node-debug.cmd or node-inspector.cmd
You should get magical words like
Node Inspector v0.12.7
Visit http://127.0.0.1:8080/?port=5858 to start debugging.
Debugger listening on port 5858
Awesome. If you know of a better solution, please let me know

Related

Issues with breakpoints in ts-node

I faced a very strange problem. I have two working PC and one of them stop breaking on the breakpoints in one day. Another PC works properly the same as before.
I tried to reinstall Node JS, ts-node and then even hard reset for windows (I use Windows) but without any success.
Then I thought that probably issue in my project and I tried to clone hello world project from google tutorial and I found that I still can't debug with it!
So I successfully run an app on my port but breakpoint won't hit. This stop working in VS Code, in WebStorm and even Chrome debugger.
This is how I run an app: "C:\Program Files\nodejs\node.exe" --inspect --require ts-node/register C:\Projects\node-typescript-starter-master\src\index.ts
Then I opened dev tools from chrome and I see this:
Then I pressed "inspect" and I see message in console:
Debugger attached. Debugger listening on ws://127.0.0.1:9229/8bae9408-867c-4278-9148-b6c90b35a8ae For help, see: https://nodejs.org/en/docs/inspector Debugger attached.
This is the next window. Seems like I missing any files overhere.
Does this mean that something is wrong with my file mapping ?
Please, let me any suggestion what can be a problem.
Well, one of workaround is to don't use ts-node and just use default node js debugger config from WebStorm with adding TypeScript prelaunch compilation script.
Here is a screenshot how to configure:

node inspector -brk not working

I am trying to use node-inspector with a simple script. I have some console logs in place so I can tell if it's running or not. I have tried 2 ways:
node-debug test.js
With this approach, the debugger opens in chrome, but does not break and runs without giving me a chance to enter break points.
In terminal window #1:
~ $ node-inspector
Node Inspector v0.9.2
Visit http://127.0.0.1:8080/debug?port=5858 to start debugging.
Open debugger in chrome. In terminal window #2:
node --debug-brk test.js
The script seems to be waiting, but the debugger window I opened has nothing going on, So I refresh the page. As soon as I do, I see my console logs (not breaking).
I'm running OSX Yosemite (10.10.1), Node.js v0.12.0, NPM 2.5.1, and Node-inspector v0.9.2.
Any ideas why this is happening?
This is a known bug when running on Node v0.12 and io.js, see https://github.com/node-inspector/node-inspector/issues/534

Node inspector debugging

I am currently having trouble getting node inspector to work with Google Chrome. I am on Windows 7 64 bit.
here is info about node-inspector:
https://github.com/node-inspector/node-inspector
when I run the following
node-debug app.js
on a simple JavaScript file called app.js:
a = 5;
b = 6;
c = a + b;
console.log(c);
and I open up Chrome with the following URL:
http://localhost:8080/debug?port=5858
all I get is this empty page with nothing loaded except one little part of the Chrome debugging toolbar, no matter what I do, I even reinstalled Chrome ( and the Canary developer version too):
Anyone have any idea what could be going wrong? This is the simplest JavaScript application ever, and there node-inspector directions does not specify any other steps to take. Like I said, I resinstalled Chrome. I have node version v0.10.30, npm version v1.4.21 version and node-inspector version: v0.7.4.
Does anyone have a good idea how I can test Chrome debugging tools without involving node? I am new to all this.
When I installed node-inspector, I did get what looks like a warning:
Try starting it without node-debug wrapper.
So, install node inspector:
npm install -g node-inspector
In one terminal:
node --debug-brk app.js
In a different terminal:
node-inspector
Browse to http://localhost:8080/debug?port=5858
i had the same issue. i then uninstalled node-inspector, installed it again. closed the cmd prompt which installed node-inspector (maybe this step made the difference). opened another cmd prompt to node-debug my.js. i can see the source file now.
I had a similar issue where going to http://localhost:8080/debug?port=5858 would result in an empty page. However going to http://127.0.0.1:8080/debug?port=5858 seems to do the trick.

How to reconnect to node.js's remote debugger

In using node.js's debugger, I've been debugging by running the node process with node --debug-brk XXXX.js. The annoying thing is, if I ever accidentally disconnect, I have to start the process all over again. Is there a way to reconnect to the debugger? When I try (via intelliJ), it simply never reconnects.
Try node-inspector it will reconnect to node server when you open it in browser but debugging will start from starting.
You can also use nodewebkit which makes it easy for debugging code.
The V8 debugger released as part of the Google Chrome Developer Tools can be used to debug Node.js scripts. A detailed explanation of how this works can be found in the Node.js GitHub wiki.
Alternatives would be
Node.js version 0.3.4+ has built-in debugging support.
node debug script.js
Manual: http://nodejs.org/api/debugger.html
Profiling with Profiler
Note: the profiler module is deprecated, and may not work with version 0.12 of node
Install globally npm install -g profiler
Start your process with node --prof this will create a v8.log file
Build nprof by running ~/.nvm/v0.8.22/lib/node_modules/profiler/tools/build-nprof
Run ~/.nvm/v0.8.22/lib/node_modules/profiler/nprof this will read the v8.log profile and give you nice output.
CPU and Memory Profiling with NodeTime
Install to your application, npm install nodetime
Include in your application, require('nodetime').profile()
Follow the instructions it will output to console
Alternatively, you may want to use look, which is based on nodetime, but it doesn't send data to nodetime.com.
Developer Tools Debugging with Node Inspector
Install it globally: npm install -g node-inspector
Run your application in debug mode: node-debug your/node/program.js (or attach to a running process: kill -s USR1 <your node process id>)
In another terminal window run node-inspector: node-inspector
Open http://127.0.0.1:8080/debug?port=5858 (or debug remotely by replacing 127.0.0.1 with your host; make sure port 8080 is open).
Webkit Developer Tools Profiling with Node Webkit Agent
Install to your application, npm install webkit-devtools-agent
Include in your application, agent = require('webkit-devtools-agent')
Activate the agent: kill -SIGUSR2 <your node process id>
Access the agent via the appropriate link

How can I debug a Sails.js app with node-inspector?

In order to debug with node-inspector I need to start my app with the node --debug command. Up to this point I have only used sails lift to start my Sails.js app, so I am unsure of how to start my app using the normal node command.
So you can actually launch a sails project with node app.js --debug if you have sails installed in your project, rather than only system-wide. Go to your project's root directory and run npm install. Sails should already be in your package.json and thus should install to your project directory.
As of Sails v0.10.x, you can do sails debug instead of sails lift.
sails inspect since Sails v1.0
As of sails v1.0, sails debug is deprecated for newer Node.js, and you should instead use sails inspect.
This is documented at: https://sailsjs.com/documentation/reference/command-line-interface/sails-inspect and is presumably done to match the newer node --inspect interface.
Have you tried using node-webkit to run your node.js apps? This is what we use at work to debug our node.js server applications. It is quite useful runtime based on chromium which you can use to inspect your code using familiar breakpoints, stack traces, variable inspection and such without having to rely on node-inspector (which I find hard to use to be honest).
What you do is instead of using console command 'node you-app.js' you set the node-webkit to launch your app, run the webkit then open its console (which is the same as console in Chrome browser) and from there you can open your source files and start debugging like any other client side JavaScript code.
node inspect
You can also use the command line debugger with:
node inspect app.js
This stops at the beginning, so do a continue:
c
And now, when your code with a statement:
debugger
gets executed, you fall into the Node CLI debugger as usual.
Tested on Sail v1.1, Node v10.15.1, Ubuntu 18.10.
nodemon --inspect and nodemon inspect
You can use those to inspect when using nodemon, which automatically reloads the app on file save: Auto reloading a Sails.js app on code changes?
Those options are analogous to node inspect and node --inspect: node inspect works with debugger statements, and node --inspect works with the Chrome debugger.
Especially useful with the "Open dedicated DevTools for Node" feature: Can I get node --inspect to open Chrome automatically
nodemon inspect is a bit annoying as it requires a continue everytime you make any app changes and nodemon restarts the server. TODO find a way around it.

Resources