1 使用最基本的 console.log()
2 使用Node debugger
Node debugger是Node内建调试工具,集成在chrome 浏览器中,调试步骤如下:
Step 1. 代码中需要调试的行位置加 debugger
,相当于执行断点:
//const duplicateNotes = notes.filter((note) => note.title === title);
const duplicateNote = notes.find((note) => note.title === title);
debugger // !相当于断点 breakpoint
if (duplicateNote) {
Step 2. 然后运行命令增加选项 inspect
,例如:
node inspect app.js add --title="title1" --body="body1"
正常运行时,命令是没有加inspect
的。如果没加此选项,那么上面代码中所加debugger
无效。
Step 3. 打开Chrome,地址栏输入: chrome://inspect
Step 4. 打开 target 下面的inspect链接,可以看到类似下面的页面,将工程文件夹添加到workspace, Esc键用于控制是否打开console,右上角的按钮类似于其他语言调试工具的step into, step out…
一旦运行结束,关闭DevTools窗口,Chrome 中的inspect也会同时消失不见,如果要重新调试,只需要在VS Code terminal 终端中输入 restart
命令,然后重新打开Chrome inspect链接。
< Waiting for the debugger to disconnec
< t...
debug> restart // restart 命令
< Debugger listening on ws://127.0.0.1:9229/2c0ca9b1-3bf
< 8-41f5-ba40-280b510dd7f5
< For help, see: https://nodejs.org/en/docs/inspector
< Debugger attached.
3 Terminal 输出的error message
通常error message第一行会指出错误所在。