如何调试 Node.js 应用程序?
如何调试 Node.js 服务器应用程序?
现在,我主要使用带有如下打印语句的警报调试:
sys.puts(sys.inspect(someVariable));
必须有更好的调试方法。我知道 Google Chrome 有一个命令行调试器。这个调试器也适用于 Node.js 吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
node-inspector 可以拯救世界!从任何支持 WebSocket 的浏览器使用它。断点、分析器、实时编码等等……这真的很棒。
安装它:
然后运行:
node-inspector could save the day! Use it from any browser supporting WebSocket. Breakpoints, profiler, livecoding, etc... It is really awesome.
Install it with:
Then run:
调试
分析
node --prof ./app.js
node --prof-process ./the- generated-log-file
堆转储
火焰图
跟踪
日志
输出调试信息的
增强堆栈跟踪信息的库
基准测试
ab -n 100000 -c 1 http ://127.0.0.1:9778/
其他
遗留
这些曾经可以工作,但不再维护或不再适用于现代节点版本。
Debugging
Profiling
node --prof ./app.js
node --prof-process ./the-generated-log-file
Heapdumps
Flamegraphs
Tracing
Logging
Libraries that output debugging information
Libraries that enhance stack trace information
Benchmarking
ab -n 100000 -c 1 http://127.0.0.1:9778/
Other
Legacy
These use to work but are no longer maintained or no longer applicable to modern node versions.
V8 调试器作为 Google Chrome 开发者工具可用于调试 Node.js 脚本。有关其工作原理的详细说明可以在 Node 中找到.js GitHub 维基。
The V8 debugger released as part of the Google Chrome Developer Tools can be used to debug Node.js scripts. A detailed explanation of how this works can be found in the Node.js GitHub wiki.
从版本 6.3 开始,Node 有自己的内置 GUI 调试器(使用 Chrome 的 DevTools)
只需传递检查器标志,您就会获得检查器的 URL:
您也可以通过传递
--inspect-brk
在第一行中断。Node has its own built in GUI debugger as of version 6.3 (using Chrome's DevTools)
Simply pass the inspector flag and you'll be provided with a URL to the inspector:
You can also break on the first line by passing
--inspect-brk
instead.Node.js 版本 0.3.4+ 具有内置的调试支持。
节点调试 script.js
手册:http://nodejs.org/api/debugger .html
Node.js version 0.3.4+ has built-in debugging support.
node debug script.js
Manual: http://nodejs.org/api/debugger.html
Visual Studio Code 将是我进行调试的选择。没有安装任何工具或 npm install 内容的开销。
只需在 package.json 中设置应用程序的起点,VSCode 就会自动在您的解决方案中创建一个配置文件。它建立在 Electron 之上,Atom 等编辑器也是在 Electron 上构建的。
Visual Studio Code will be my choice for debugging. No overhead of installing any tools or
npm install
stuff.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.
我个人使用 JetBrains WebStorm,因为它是我发现的唯一适用于前端和后端 JavaScript 的 JavaScript IDE。
它可以在多个操作系统上运行,并且内置 Node.js 调试(以及大量其他内容)](http ://www.jetbrains.com/webstorm/features/index.html)。
我唯一的“问题”/愿望清单项目
是是:在 Mac 上,它似乎比 Windows 上更需要资源在版本 6 中,这似乎不再是问题。如果它有 Snippet 支持,那就太好了(就像 Sublime Text 2 - 即输入“fun”并点击“tab”以放入函数。请参阅下面的 @WickyNilliams 评论 - 使用实时模板,您还可以获得代码片段支持。I personally use JetBrains WebStorm as it's the only JavaScript IDE that I've found which is great for both frontend and backend JavaScript.
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).
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.这里有很多很棒的答案,但我想添加我的观点(基于我的方法的演变)
调试日志
让我们面对现实,我们都喜欢一个好的
console.log('Uh oh, if you come here ,你最好跑。')
有时这效果很好,所以如果你不愿离它太远,至少用 Visionmedia 的调试。交互式调试
尽管控制台日志记录很方便,但要进行专业调试,您需要卷起袖子并投入其中。设置断点,单步执行代码,检查范围和变量以查看导致奇怪行为的原因。正如其他人提到的, node-inspector 确实是最重要的。它可以完成您可以使用内置调试器完成的所有操作,但使用的是熟悉的 Chrome DevTools 界面。
如果像我一样,您使用 Webstorm,则 这里是从那里进行调试的便捷指南。
堆栈跟踪
默认情况下,我们无法跟踪事件循环不同周期(滴答)中的一系列操作。要解决这个问题,请查看 longjohn (但不在生产环境中!)。
内存泄漏
使用 Node.js,我们可以让服务器进程保持相当长的时间。如果您认为出现了一些令人讨厌的泄密事件,您会怎么做?使用 heapdump 和 Chrome DevTools 比较一些快照并查看发生了什么变化。
有关一些有用的文章,请查看
如果您想观看视频,请
无论您选择什么路径,只要确保您了解如何调试
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.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-inspector really 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 here is a handy guide to debugging from there.
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!).
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 heapdump and Chrome DevTools to compare some snapshots and see what's changing.
For some useful articles, check out
If you feel like watching a video(s) then
Whatever path you choose, just be sure you understand how you are debugging
Theseus 是 Adobe 研究的一个项目,可让您在开源编辑器中调试 Node.js 代码括号。它有一些有趣的功能,如实时代码覆盖率、追溯检查、异步调用树。
Theseus is 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.
适用于 Visual Studio 的 Node.js 工具 2012 或 2013 包含一个调试器。 此处的概述指出“适用于 Visual Studio 的 Node.js 工具包括对调试节点应用程序的完整支持。”。作为 Node.js 新手,但具有 .NET 背景,我发现此插件是调试 Node.js 应用程序的好方法。
Node.js Tools for Visual Studio 2012 or 2013 includes a debugger. The overview here states "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 Studio Code 具有非常好的 Node.js 调试支持。它是免费、开源和跨平台的,可在 Linux、OS X 和 Windows 上运行。
您甚至可以调试grunt 和 gulp 任务,如果您需要...
Visual Studio Code has really nice Node.js debugging support. It is free, open source and cross-platform and runs on Linux, OS X and Windows.
You can even debug grunt and gulp tasks, should you need to...
我编写了一种不同的方法来调试 Node.js 代码,该方法稳定且非常简单。它位于 https://github.com/sa/iron-node。
开源跨平台可视化调试器。
安装:
npm install iron-node -g;
调试:
iron-node yourscript.js;
I wrote a different approach to debug Node.js code which is stable and is extremely simple. It is available at https://github.com/s-a/iron-node.
An opensource cross-platform visual debugger.
Installation:
npm install iron-node -g;
Debug:
iron-node yourscript.js;
我创建了一个名为 pry.js 的简洁小工具,可以帮助您。
在代码中的某个位置放置一个简单的语句,正常运行脚本,节点将停止当前线程,让您可以访问所有变量和函数。随意查看/编辑/删除它们!
I created a neat little tool called pry.js that can help you out.
Put a simple statement somewhere in your code, run your script normally and node will halt the current thread giving you access to all your variables and functions. View/edit/delete them at will!
如果您使用 Atom IDE,则可以安装
node-debugger
包。If you are using the Atom IDE, you can install the
node-debugger
package.使用 Chrome 版本 67.0.3396.62(+)
将会弹出另一个 DevTools 窗口,专门用于调试节点应用程序。
Using Chrome Version 67.0.3396.62(+)
There will be another DevTools window that will pop out specifically for debugging node app.
Node.js 中有内置的命令行 调试器客户端。 Cloud 9 IDE 还有相当不错的(视觉)调试器。
There is built-in command line debugger client within Node.js. Cloud 9 IDE have also pretty nice (visual) debugger.
我整理了一个关于使用 Node.js 调试入门 “https://github.com/node-inspector/node-inspector” rel="nofollow noreferrer">node-inspector 适合那些不确定从哪里开始的人。
I put together a short Node.js debugging primer on using the node-inspector for those who aren't sure where to get started.
Visual Studio Code 可以帮助我们进行调试。
Visual Studio Code will work for us in debugging.
使用网络风暴!它非常适合调试 Node.js 应用程序。它有一个内置的调试器。查看此处的文档: https://www .jetbrains.com/help/webstorm/2016.1/running-and-debugging-node-js.html
Use Webstorm! It's perfect for debugging Node.js applications. It has a built-in debugger. Check out the docs here: https://www.jetbrains.com/help/webstorm/2016.1/running-and-debugging-node-js.html
如果您需要一个强大的 Node.js 日志库,Tracer https://github.com/baryon/tracer 是一个更好的选择。
它输出带有时间戳、文件名、方法名、行号、路径或调用堆栈的日志消息,支持彩色控制台,并轻松支持数据库、文件、流传输。我是作者。
If you need a powerful logging library for Node.js, Tracer https://github.com/baryon/tracer is a better choice.
It outputs log messages with a timestamp, file name, method name, line number, path or call stack, support color console, and support database, file, stream transport easily. I am the author.
假设您的计算机上安装了 node-inspector(如果没有,只需输入“npm install -g node-inspector”),您只需运行:
并将命令行中的 URI 粘贴到 WebKit (Chrome / Safari) 浏览器中。
Assuming you have node-inspector installed on your computer (if not, just type 'npm install -g node-inspector') you just have to run:
And paste the URI from the command line into a WebKit (Chrome / Safari) browser.
只是为了完整起见:
PyCharm 3.0 + Node.js插件提供了很棒的开发+运行+调试体验。
Just for completeness:
The PyCharm 3.0 + Node.js Plugin offers an awesome development + run + debug experience.
使用 --inspect 标志启动节点进程。
node --inspect index.js
,然后在 chrome 中打开
chrome://inspect
。点击“打开 Node 专用开发工具”链接或安装此 chrome 扩展,用于轻松打开 chrome DevTools。有关更多信息,请参阅此链接
Start your node process with --inspect flag.
node --inspect index.js
and then Open
chrome://inspect
in chrome. Click the "Open dedicated DevTools for Node" link or install this chrome extension for easily opening chrome DevTools.For more info refer to this link
有新的开源 Nodeclipse 项目(作为 Eclipse 插件或 Enide Studio):
Nodeclipse 在 2013 年 Eclipse 十大新插件。它使用修改后的 V8 调试器(来自 Google Chrome Developer Tools for Java)。
Nodeclipse 是免费的开源软件每月月初发布。
There is the new open-source Nodeclipse project (as a Eclipse plugin or Enide Studio):
Nodeclipse became #1 in Eclipse Top 10 NEW Plugins for 2013. It uses a modified V8 debugger (from Google Chrome Developer Tools for Java).
Nodeclipse is free open-source software released at the start of every month.
有很多可能性...
调试支持通常使用 v8 来实现调试协议或较新的 Chrome 调试协议。
There are many possibilities...
Debug support is often implemented using the v8 Debugging Protocol or the newer Chrome Debugging Protocol.
IntelliJ 非常适合 Node.js。
此外,IntelliJ 很好地支持“代码帮助”。
IntelliJ works wonderfully for Node.js.
In addition, IntelliJ supports 'Code Assistance' well.
NetBeans IDE 自 版本 8.1:
其他参考:
The NetBeans IDE has had Node.js support since version 8.1:
Additional references:
使用这个命令
Use this commands
ndb 是一种改进的 Node.js 调试体验,由 Chrome DevTools 启用
https://github.com/GoogleChromeLabs/ndb
ndb is an improved debugging experience for Node.js, enabled by Chrome DevTools
https://github.com/GoogleChromeLabs/ndb