node.js调试
2016-10-25 11:30
155 查看
node.js是基于google V8引擎打造的基于事件循环的异步框架。它的优势在于他的简单,里面有两个非常重要的库:express、socket.io,而且社区的npm也有很多三方库。里面的异步方式的调用的确写起来也非常舒服。“吃大饼难免掉芝麻,写程序难免有bug。”本文用于介绍node.js在写完之后调试的套路。
从这个图片可以看出来,node.js在启动的时候将会打开一个端口,这个主要是提供给
程序开启调试模式之后,将会自动断到程序的第一句。下面就是支持的命令列表:
常用的命令有:
最后的效果:
内置调试器
这里只是介绍一下基本的用法。实例代码,先输出hello,在1秒之后输出world。x=5; setTimeout(function(){ debugger; console.log("world"); },1000); console.log("hello");
如何加载调试器
从这个图片可以看出来,node.js在启动的时候将会打开一个端口,这个主要是提供给
程序开启调试模式之后,将会自动断到程序的第一句。下面就是支持的命令列表:
debug> help Commands: run (r), cont (c), next (n), step (s), out (o), backtrace (bt), setBreakpoint (sb), clearBreakpoint (cb), watch, unwatch, watchers, repl, restart, kill, list, scripts, breakOnException, breakpoints, version
常用的命令有:
c continue n next s step sb("filename",line_num) sb()当前行断点 sb(line)当前文件linue行设置断点 sb("fun()") bt 调用堆栈打印。 repl 切换成交互模式,这个时候才能打印输出,ctrl+c才能退出来。 pause 暂停 scripts 显示当前已加载的所有脚本 watchers/watch/unwatch 关于观察点的操作,看字面上也能理解。
break in F:\learn\node.js\test.js:3 1 x=5; 2 setTimeout(function(){ > 3 debugger; 4 console.log("world"); 5 },1000); debug> repl Press Ctrl + C to leave debug repl > x 5 debug> debug> bt #0 test.js:3:5
使用Node Inspector调试
这个是一个可视化的调试工具,通过chrome就能来调试。安装
安装的时候可能遇到翻墙的问题。可以使用这个命令来改变自己的源。D:\WINDOWS\system32>npm install --registry=http://r.cnpmjs.org -g node-inspector \ > v8-debug@0.7.7 preinstall D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector\node_modules\v8-debug > node -e 'process.exit(0)' / > v8-profiler@5.6.5 preinstall D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector\node_modules\v8-profiler > node -e 'process.exit(0)' npm WARN deprecated has-color@0.1.7: Renamed to supports-color. If you're using chalk, upgrade to the latest version. https://github.com/chalk/supports-color npm WARN deprecated lodash@2.4.2: lodash@<3.0.0 is no longer maintained. Upgrade to lodash@^4.0.0. \ > v8-profiler@5.6.5 install D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector\node_modules\v8-profiler > node-pre-gyp install --fallback-to-build [v8-profiler] Success: "D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector\node_modules\v8-profiler\build\profiler\v5.6.5\node-v14-win32-ia32\profiler.node" already installed Pass --update-binary to reinstall or --build-from-source to recompile > v8-debug@0.7.7 install D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector\node_modules\v8-debug > node-pre-gyp install --fallback-to-build [v8-debug] Success: "D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector\node_modules\v8-debug\build\debug\v0.7.7\node-v14-win32-ia32\debug.node" is installed via remote D:\Users\Administrator\AppData\Roaming\npm\node-inspector -> D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector\bin\inspector.js D:\Users\Administrator\AppData\Roaming\npm\node-debug -> D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector\bin\node-debug.js node-inspector@0.12.8 D:\Users\Administrator\AppData\Roaming\npm\node_modules\node-inspector ├── path-is-absolute@1.0.1 ├── async@0.9.2 ├── semver@4.3.6 ├── which@1.2.11 (isexe@1.1.2) ├── strong-data-uri@1.0.4 (truncate@1.0.5) ├── debug@2.2.0 (ms@0.7.1) ├── serve-favicon@2.3.0 (fresh@0.3.0, parseurl@1.3.1, ms@0.7.1, etag@1.7.0) ├── rc@1.1.6 (ini@1.3.4, deep-extend@0.4.1, strip-json-comments@1.0.4, minimist@1.2.0) ├── ws@1.1.1 (options@0.0.6, ultron@1.0.2) ├── glob@5.0.15 (inherits@2.0.3, inflight@1.0.6, once@1.4.0, minimatch@3.0.3) ├── yargs@3.32.0 (decamelize@1.2.0, camelcase@2.1.1, y18n@3.2.1, window-size@0.1.4, string-width@1.0.2, cliui@3.2.0, os-locale@1.4.0) ├── express@4.14.0 (escape-html@1.0.3, array-flatten@1.1.1, fresh@0.3.0, utils-merge@1.0.0, cookie-signature@1.0.6, merge-descriptors@1.0.1, methods@1.1.2, parseurl@1.3.1, etag@1.7.0, path-to-regexp@0.1.7, content-type@1.0.2, encodeurl@1.0.1, vary@1.1.0, range-parser@1.2.0, content-disposition@0.5.1, cookie@0.3.1, serve-static@1.11.1, depd@1.1.0, qs@6.2.0, on-finished@2.3.0, finalhandler@0.5.0, proxy-addr@1.1.2, accepts@1.3.3, type-is@1.6.13, send@0.14.1) ├── v8-profiler@5.6.5 (nan@2.4.0, node-pre-gyp@0.6.31) ├── biased-opener@0.2.8 (minimist@1.2.0, browser-launcher2@0.4.6, x-default-browser@0.3.1) └── v8-debug@0.7.7 (nan@2.4.0, node-pre-gyp@0.6.31)
启动node-inspector
在调试期间都需要这个进程来提供外部调用。F:\learn\node.js>node-inspector --web-port=8888 test Node Inspector v0.12.8 Visit http://127.0.0.1:8888/?port=5858 to start debugging. Cannot send response - there is no front-end connection.
启动node调试器
需要再开一个console来启动我们的test.js的脚本F:\learn\node.js>node debug test.js < Debugger listening on port 5858 connecting to port 5858... ok break in F:\learn\node.js\test.js:1 > 1 x=5; 2 setTimeout(function(){ 3 debugger; debug>
通过chrome开始调试
http://127.0.0.1:8888/?port=5858
最后的效果:
相关文章推荐
- 调试Node.JS的辅助工具(NodeWatcher)
- 用Eclipse调试Node.js代码
- 用Eclipse调试Node.js代码
- node.js第六课(包管理器和代码调试)
- 在Visual Studio上开发Node.js程序(2)——远程调试及发布到Azure
- 用Eclipse调试Node.js代码
- node.js调试
- 使用webstorm调试node.js
- 使用sublime调试node.js
- Node.js调试
- node.js 调试工具
- 【Node.js】环境配置及调试方法
- Node.js调试工具安装使用
- node.js调试
- 使用Eclipse调试Node.js代码
- 调试Node.JS的辅助工具(NodeWatcher)
- node.js 调试注意项
- 【ask】webstorm调试node单个js文件
- 在Visual Studio上开发Node.js程序(2)——远程调试及发布到Azure
- node.js调试