Javascript 如何调试 Node.js 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1911015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I debug Node.js applications?
提问by Fabian Jakobs
How do I debug a Node.js server application?
如何调试 Node.js 服务器应用程序?
Right now I'm mostly using alert debuggingwith print statements like this:
现在我主要使用带有打印语句的警报调试,如下所示:
sys.puts(sys.inspect(someVariable));
There must be a better way to debug. I know that Google Chromehas a command-line debugger. Is this debugger available for Node.js as well?
一定有更好的调试方法。我知道谷歌浏览器有一个命令行调试器。这个调试器也可用于 Node.js 吗?
采纳答案by Fabian Jakobs
The V8debugger released as part of the Google Chrome Developer Toolscan be used to debug Node.js scripts. A detailed explanation of how this works can be found in the Node.js GitHub wiki.
作为 Google Chrome Developer Tools 的一部分发布的V8调试器可用于调试 Node.js 脚本。可以在Node.js GitHub wiki 中找到有关其工作原理的详细说明。
回答by daralthus
node-inspectorcould save the day! Use it from any browser supporting WebSocket. Breakpoints, profiler, livecoding, etc... It is really awesome.
节点检查员可以节省一天!从任何支持WebSocket 的浏览器中使用它。断点、分析器、实时编码等……真是太棒了。
Install it with:
安装它:
npm install -g node-inspector
Then run:
然后运行:
node-debug app.js
回答by balupton
Debugging
调试
Profiling
剖析
node --prof ./app.jsnode --prof-process ./the-generated-log-file
node --prof ./app.jsnode --prof-process ./the-generated-log-file
Heapdumps
堆转储
Flamegraphs
火焰图
- 0x
- jam3/devtoolthen Chrome Developer Tools Flame Charts
- Dtraceand StackVis— Only supported on SmartOS
- clinicjs
Tracing
追踪
Logging
日志记录
Libraries that output debugging information
输出调试信息的库
Libraries that enhance stack trace information
增强堆栈跟踪信息的库
Benchmarking
基准测试
- Apache Bench:
ab -n 100000 -c 1 http://127.0.0.1:9778/ - wrk
Other
其他
Legacy
遗产
These use to work but are no longer maintained or no longer applicable to modern node versions.
这些曾经可以工作,但不再维护或不再适用于现代节点版本。
- https://github.com/bnoordhuis/node-profiler- replaced by built-in debugging
- https://github.com/c4milo/node-webkit-agent- replaced by node inspector
- https://nodetime.com/- defunct
回答by Alister
Node has its own built inGUI debugger as of version 6.3 (using Chrome's DevTools)
从 6.3 版开始,Node 有自己的内置GUI 调试器(使用 Chrome 的 DevTools)
Simply pass the inspector flag and you'll be provided with a URL to the inspector:
只需传递检查员标志,您就会获得检查员的 URL:
node --inspect server.js
You can also break on the first line by passing --inspect-brkinstead.
您也可以通过传递在第一行中断--inspect-brk。
回答by JulianW
Node.js version 0.3.4+ has built-in debugging support.
Node.js 0.3.4+ 版本具有内置的调试支持。
node debug script.js
node debug script.js
回答by Shreyas
Visual Studio Codewill be my choice for debugging. No overhead of installing any tools or npm installstuff.
Just set the starting point of your app in package.json and VSCode will automatically create a configuration file inside your solution. It's build on Electron, on which editors like Atom are built.
Visual Studio Code将是我调试的选择。没有安装任何工具或npm install东西的开销。只需在 package.json 中设置您的应用程序的起点,VSCode 将自动在您的解决方案中创建一个配置文件。它建立在Electron之上,Atom 等编辑器也建立在Electron之上。
VS Code gives similar debugging experience as you might have had in other IDEs like VS, Eclipse, etc.
VS Code 提供了与您在 VS、Eclipse 等其他 IDE 中可能拥有的类似调试体验。
回答by isNaN1247
I personally use JetBrains WebStormas it's the only JavaScript IDE that I've found which is great for both frontend and backend JavaScript.
我个人使用JetBrains WebStorm,因为它是我发现的唯一一个同时适用于前端和后端 JavaScript 的 JavaScript IDE。
It works on multiple OS's and has Node.js debugging built-in (as well as a ton of other stuff](http://www.jetbrains.com/webstorm/features/index.html).
它适用于多个操作系统,并内置了 Node.js 调试(以及大量其他东西)(http://www.jetbrains.com/webstorm/features/index.html)。
My only 'issues'/wishlist items arewere:
我唯一的“问题”/愿望清单项目 是是:
It seems to be more resource hungry on Mac than WindowsIt no longer seems an issue in version 6.It would be nice if it had Snippet support (like those of Sublime Text 2- i.e. type 'fun' and tap 'tab' to put in a function.See @WickyNilliams comment below - With Live Templates you also have snippet support.
Mac 上似乎比 Windows 更需要资源在第 6 版中似乎不再是问题。如果它有 Snippet 支持(就像Sublime Text 2 的那些- 即输入“fun”并点击“tab”来输入一个函数,那就太好了。请参阅下面的@WickyNilliams 评论 - 使用实时模板,您还可以获得代码段支持。
回答by Philip O'Brien
A lot of great answers here, but I'd like to add my view (based on how my approach evolved)
这里有很多很棒的答案,但我想添加我的观点(基于我的方法的演变方式)
Debug Logs
调试日志
Let's face it, we all love a good console.log('Uh oh, if you reached here, you better run.')and sometimes that works great, so if you're reticent to move too far away from it at least add some bling to your logs with Visionmedia's debug.
让我们面对console.log('Uh oh, if you reached here, you better run.')现实吧,我们都喜欢好的,有时效果很好,所以如果您不愿远离它,至少使用Visionmedia 的 debug为您的日志添加一些亮点。
Interactive Debugging
交互式调试
As handy as console logging can be, to debug professionally you need to roll up your sleeves and get stuck in. Set breakpoints, step through your code, inspect scopes and variables to see what's causing that weird behaviour. As others have mentioned, node-inspectorreally is the bees-knees. It does everything you can do with the built-in debugger, but using that familiar Chrome DevTools interface. If, like me, you use Webstorm, then hereis a handy guide to debugging from there.
尽管控制台日志记录很方便,但要进行专业调试,您需要卷起袖子并陷入困境。设置断点,单步执行代码,检查范围和变量以查看导致这种奇怪行为的原因。正如其他人所提到的,节点检查器确实是蜜蜂膝盖。它使用内置调试器完成您可以做的所有事情,但使用熟悉的 Chrome DevTools 界面。如果你像我一样使用Webstorm,那么这里有一个方便的调试指南。
Stack Traces
堆栈跟踪
By default, we can't trace a series of operations across different cycles of the event loop (ticks). To get around this have a look at longjohn(but not in production!).
默认情况下,我们无法在事件循环的不同周期(滴答)中跟踪一系列操作。要解决这个问题,请查看longjohn(但不在生产中!)。
Memory Leaks
内存泄漏
With Node.js we can have a server process expected to stay up for considerable time. What do you do if you think it has sprung some nasty leaks? Use heapdumpand Chrome DevTools to compare some snapshots and see what's changing.
使用 Node.js,我们可以让服务器进程保持相当长的时间。如果您认为它出现了一些令人讨厌的泄漏,您会怎么做?使用heapdump和 Chrome DevTools 比较一些快照,看看有什么变化。
For some useful articles, check out
对于一些有用的文章,请查看
If you feel like watching a video(s) then
如果您想观看视频,那么
- Netflix JS Talks - Debugging Node.js in Production
- Interesting videofrom the tracing working groupon tracing and debugging node.js
- Really informative 15-minute video on node-inspector
Whatever path you choose, just be sure you understand how you are debugging
无论你选择什么路径,只要确保你了解你是如何调试的
It is a painful thing
To look at your own trouble and know
That you yourself and no one else has made itSophocles, Ajax
看着自己的麻烦,知道
自己和其他人没有做到, 这是一件痛苦的事情索福克勒斯,阿贾克斯
回答by Sindre Sorhus
Theseusis a project by Adobe research which lets you debug your Node.js code in their Open Source editor Brackets. It has some interesting features like real-time code coverage, retroactive inspection, asynchronous call tree.
忒修斯是 Adobe 研究的一个项目,它允许您在他们的开源编辑器Brackets 中调试您的 Node.js 代码。它有一些有趣的特性,比如实时代码覆盖、追溯检查、异步调用树。


回答by John81
Node.js Tools for Visual Studio2012 or 2013 includes a debugger. The overview herestates "Node.js Tools for Visual Studio includes complete support for debugging node apps.". Being new to Node.js, but having a background in .NET, I've found this add in to be a great way to debug Node.js applications.
适用于 Visual Studio2012 或 2013 的Node.js 工具包括一个调试器。此处的概述指出“适用于 Visual Studio 的 Node.js 工具包括对调试节点应用程序的完整支持。”。作为 Node.js 的新手,但有 .NET 背景,我发现这个插件是调试 Node.js 应用程序的好方法。

