- Using console object: console.log('xxx');
- Using Node.js internal debugger (v8 engine): node debug xxx.js
- Using node-inspector: https://github.com/node-inspector/node-inspector
How to use node-inspector:
- In one terminal, run "node-inspector" to start the node inspector server.
- In another terminal, run
node --debug your/node/program.js
-
Or
node --debug-brk your/short/node/script.js // pause your script on the first line
to start your node process. - Launch Chrome or Opera, open http://127.0.0.1:8080/debug?port=5858 to launch debugger UI. (Server code can be shown here)
- You can set breakpoint to code (although you may cannot see the breakpoint first time, click "Refresh" button on browser, you can see program will stop at the breakpoint you set last time.)
- Launch another browser thread, input something to debug (for example, http://127.0.0.1:1337/ which you defined in node server).
Another easy way to debug Node.js is using VS code:
- Open a folder containing server.js in VS code.
- In Debug tab, click "Configure or Fix 'launch.json'" to select "Node.js" and a "launch.json" file will be generated for debugging.
- Click "Start Debugging" to debug.