➜ Desktop node debug test.js (node:78403) [DEP0068] DeprecationWarning: `node debug` is deprecated. Please use `node inspect` instead. < Debugger listening on ws://127.0.0.1:9229/eaab7bad-ccd1-46d8-9e82-d5845e864dd7 < For help see https://nodejs.org/en/docs/inspector < Debugger attached. Break on start in test.js:1 > 1 (function (exports, require, module, __filename, __dirname) { var a = 123; 2 console.log(a); 3 debug> help run, restart, r Run the application or reconnect kill Kill a running application or disconnect
cont, c Resume execution next, n Continue to next line in current file step, s Step into, potentially entering a function out, o Step out, leaving the current function backtrace, bt Print the current backtrace list Print the source around the current line where execution is currently paused
setBreakpoint, sb Set a breakpoint clearBreakpoint, cb Clear a breakpoint breakpoints List all known breakpoints breakOnException Pause execution whenever an exception is thrown breakOnUncaught Pause execution whenever an exception isn't caught breakOnNone Don't pause on exceptions (this is the default)
watch(expr) Start watching the given expression unwatch(expr) Stop watching an expression watchers Print all watched expressions and their current values
exec(expr) Evaluate the expression and print the value repl Enter a debug repl that works like exec
scripts List application scripts that are currently loaded scripts(true) List all scripts (including node-internals)
profile Start CPU profiling session. profileEnd Stop current CPU profiling session. profiles Array of completed CPU profiling sessions. profiles[n].save(filepath = 'node.cpuprofile') Save CPU profiling session to disk as JSON.
takeHeapSnapshot(filepath = 'node.heapsnapshot') Take a heap snapshot and save to disk as JSON. debug>
➜ Desktop node inspect test.js < Debugger listening on ws://127.0.0.1:9229/e0ac6cc0-d7db-40ec-bd58-4d9a98f2a18f < For help see https://nodejs.org/en/docs/inspector < Debugger attached. Break on start in test.js:1 > 1 (function (exports, require, module, __filename, __dirname) { var a = 123; 2 console.log(a); 3 debug> help run, restart, r Run the application or reconnect kill Kill a running application or disconnect
cont, c Resume execution next, n Continue to next line in current file step, s Step into, potentially entering a function out, o Step out, leaving the current function backtrace, bt Print the current backtrace list Print the source around the current line where execution is currently paused
setBreakpoint, sb Set a breakpoint clearBreakpoint, cb Clear a breakpoint breakpoints List all known breakpoints breakOnException Pause execution whenever an exception is thrown breakOnUncaught Pause execution whenever an exception isn't caught breakOnNone Don't pause on exceptions (this is the default)
watch(expr) Start watching the given expression unwatch(expr) Stop watching an expression watchers Print all watched expressions and their current values
exec(expr) Evaluate the expression and print the value repl Enter a debug repl that works like exec
scripts List application scripts that are currently loaded scripts(true) List all scripts (including node-internals)
profile Start CPU profiling session. profileEnd Stop current CPU profiling session. profiles Array of completed CPU profiling sessions. profiles[n].save(filepath = 'node.cpuprofile') Save CPU profiling session to disk as JSON.
takeHeapSnapshot(filepath = 'node.heapsnapshot') Take a heap snapshot and save to disk as JSON. debug>
感兴趣,自己尝试了。官方的推荐,但不够可视化。
使用Node inspector
使用node-inspector
$ npm install node-inspector -g
然后需要通过浏览器连接到node-inspector,需要启动inspector服务:
$ node-inspector 最后以debug模式运行node.js应用:
官文使用方法: 实际已不推荐。 $ node --debug app.js
ps: The node-debug command will load Node Inspector in your default browser.
NOTE: Node Inspector works in Chrome and Opera only. You have to re-open the inspector page in one of those browsers if another browser is your default web browser (e.g. Safari or Internet Explorer).