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.
Related
I added in package.json this into scripts:
"debug": "NODE_PATH=src nodemon --exec babel-node src/run.js --inspect",
It is pretending that it is debugging but jumping on selected lines how it pleases. I'm not that "expert" in JavaScript (I'm Java) but really this is pain in ass.
How can I debug backend without debug? Srry I'm angry because this is second time I'm giving change to JavaScript and this is second time I'm furious about this stupidity.
Thanks for any hints.
P.S.: if there is better software, tool to debug please just refer to it
In WebStorm, the easiest way to debug the application started via NPM stript is using the icon in the gutter: open your package.json in editor, right-click the icon to the left of your script and choose debug:
See also https://blog.jetbrains.com/webstorm/2017/09/debugging-node-js-apps-in-webstorm/
Note that:
babel-node is deprecated and not recommended for using in production. To get ES6 code compiled on-the-fly, try running node with -r #babel/register. see https://babeljs.io/docs/en/babel-register
by running nodemon --exec babel-node src/run.js --inspect, you pass --inspect to your application, not to node interpreter, so this command doesn't start the debugger. Node options have to be specified before the javascript file, otherwise the passed options will be treated as application arguments, not as node.js args
Right now, VSCode(Visual Studio Code) is considered as best tool for development, which is light weight and user friendly.
You can get different extensions based on your requirement.
To debug node js in vscode, go to debug window -> add configuration -> type attach process -> press ctrl + space -> press enter on attach suggestion.
After that, run command, node "your file name" --inspect
Press F5, your debugger will be attached
Node js debugging using VSCode
I am using Angular Universal Starter repo. In angular 4 universal for ssr we could debug browser and node part of application in dev mode(see console), but now I do not see way to debug node part. I tried to execute ts-node server.ts with some changes( paths to files, etc), but angular seems needs aot compiled app and
throw Error: You must pass in a NgModule or NgModuleFactory to be
bootstrapped.
from docs:
Development (Client-side only rendering)
- run npm run start which will start ng serve
Production (also for testing SSR/Pre-rendering locally)
- npm run build:ssr && npm run serve:ssr
At first glance debug on Node.js in development do not work. At least from the box. May be someone resolve this issue.
You can't debug node part of your Angular 4 app in browser. Node work on server part so you can't see this in browser (client side).
Only way to debug this part when you start it from ts-node server.ts is to use external tools like WebStorm etc. If you start your App in TS mode from Debug mode you can use all features of this tools.
I think this small piece of code can help you
create the project
ng n debuggable-universal-server --interactive=false
cd debuggable-universal-server
add universal
ng add #nguniversal/express-engine --clientProject debuggable-universal-server
To create the server-side app module, app.server.module.ts, run the following CLI command.
ng add #nguniversal/express-engine
To start rendering your app with Universal on your local system, use the following command.
npm run dev:ssr
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
It's a fairly small thing, but it feels like I'm manually refreshing the node-inspector tab in chrome a million times a day, and there must be a better way.
When a file changes, and node restarts, and node-inspector detaches from target -- is there a way for it to automatically re-attach itself?
This question is a duplicate of How can I make node-inspector restart when node is restarted?. See the accepted answer for a workaround solution using GreaseMonkey.
There is also a GitHub issue filled in the Node Inspector project: #266.
Here's instructions to have a Node console (not REPL), while using nodemon, with a server output console, all from within VSCode.
Node.js debugging in VS Code with Nodemon
The only thing to look out for is that it needs to be started manually with
nodemon --inspect ./bin/www
You can't let nodemon use your package.json start defaults because it won't restart.
The only thing this lacks is a webpage restart (if you're using web front end) but that's a whole other question.
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