整理一下node的debug方式
======================================================
之前网上搜索到资料是在终端中使用以下命令运行js:
node debug index.js
node --debug index.js
node --debug-brk index.js
三者的区别可以自行搜索,但是我用的node版本是12,运行以上命令会有以下提示:
DeprecationWarning: `node debug` is deprecated. Please use `node inspect` instead.
`node --debug` and `node --debug-brk` are invalid. Please use `node --inspect` and `node --inspect-brk` instead.
========================================================
可见debug要么弃用要么无效,已经被inspect取代了,对应着以下命令:
node inspect index.js
node --inspect index.js
node --inspect-brk index.js
node --inspect 与 node --inspect-brk的区别在后面的内容会有提及。
index.js部分代码,启动成功之后打印一条信息:
app.listen(config.port, function () {
console.log(`${pkg.name} listening on port ${config.port}`)
});
调试node,分为两种方式:1、借助chrome开发者工具;2、使用命令行。
node --inspect | node --inspect-brk | |
开发者工具 | ||
node inspect |
因为使用命令行进行调试个人感觉不友好,所以不做介绍,只介绍如何使用开发者工具进行调试
一、使用node --inspect:
1、运行node --inspect index.js
正在监听9229,同时打印了语句,说明已经成功运行了。
2、在chrome地址栏输入:chrome://inspect
稍等
3、当Remote Target出现内容之后,可以看到当前在运行的js,点击inspect,弹出nodejs专用的开发者工具,同时终端输出信息有变化
4、在Sources面板,手动选中项目目录,并运行开发者工具访问
5、后面的调试步骤就和调试web项目一样了。
还有更另外一种方式打开nodejs专用的开发者工具,首先打开开发者工具,点击nodejs图标,效果与上面一样
二、使用node --inspect-brk:
1、运行node --inspect-brk index.js
同样也是监听了一个开发展工具得默认端口,但是并没有打印我们要输出得语句,这一点与node --inspect是有区别的。
2、直接打开开发者工具,点击nodejs图标,会发现,自动在第一行加了一个断点,这个也是与node --inspect是有区别的。
后面就可以按自己的步骤进行调试了。
当然有一些优秀的IDE也提供了debug模式,比如IDEA、VS Code等,这里也不再赘述了。