如何调试 Node.js 应用程序?

发布于 2024-08-15 04:14:17 字数 273 浏览 9 评论 0 原文

如何调试 Node.js 服务器应用程序?

现在,我主要使用带有如下打印语句的警报调试

sys.puts(sys.inspect(someVariable));

必须有更好的调试方法。我知道 Google Chrome 有一个命令行调试器。这个调试器也适用于 Node.js 吗?

How do I debug a Node.js server application?

Right now I'm mostly using alert debugging with print statements like this:

sys.puts(sys.inspect(someVariable));

There must be a better way to debug. I know that Google Chrome has a command-line debugger. Is this debugger available for Node.js as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

风铃鹿 2024-08-22 04:14:18

node-inspector 可以拯救世界!从任何支持 WebSocket 的浏览器使用它。断点、分析器、实时编码等等……这真的很棒。

安装它:

npm install -g node-inspector

然后运行:

node-debug app.js

node-inspector could save the day! Use it from any browser supporting WebSocket. Breakpoints, profiler, livecoding, etc... It is really awesome.

Install it with:

npm install -g node-inspector

Then run:

node-debug app.js
东京女 2024-08-22 04:14:18

调试

分析

  1. node --prof ./app.js
  2. node --prof-process ./the- generated-log-file

堆转储

火焰图

跟踪

日志

输出调试信息的

增强堆栈跟踪信息的库

基准测试

其他

遗留

这些曾经可以工作,但不再维护或不再适用于现代节点版本。

Debugging

Profiling

  1. node --prof ./app.js
  2. node --prof-process ./the-generated-log-file

Heapdumps

Flamegraphs

Tracing

Logging

Libraries that output debugging information

Libraries that enhance stack trace information

Benchmarking

Other

Legacy

These use to work but are no longer maintained or no longer applicable to modern node versions.

最舍不得你 2024-08-22 04:14:18

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.

皓月长歌 2024-08-22 04:14:18

从版本 6.3 开始,Node 有自己的内置 GUI 调试器(使用 Chrome 的 DevTools)

节点内置 GUI 调试器

只需传递检查器标志,您就会获得检查器的 URL:

node --inspect server.js

您也可以通过传递 --inspect-brk 在第一行中断。

Node has its own built in GUI debugger as of version 6.3 (using Chrome's DevTools)

Nodes builtin GUI debugger

Simply pass the inspector flag and you'll be provided with a URL to the inspector:

node --inspect server.js

You can also break on the first line by passing --inspect-brk instead.

不再让梦枯萎 2024-08-22 04:14:18

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

甜警司 2024-08-22 04:14:18

Visual Studio Code 将是我进行调试的选择。没有安装任何工具或 npm install 内容的开销。
只需在 package.json 中设置应用程序的起点,VSCode 就会自动在您的解决方案中创建一个配置文件。它建立在 Electron 之上,Atom 等编辑器也是在 Electron 上构建的。

VS Code 提供与您类似的调试体验
VS、Eclipse 等其他 IDE 中都有。

在此处输入图像描述
输入图像描述这里

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.

VS Code gives similar debugging experience as you might have
had in other IDEs like VS, Eclipse, etc.

enter image description here
enter image description here

上课铃就是安魂曲 2024-08-22 04:14:18

我个人使用 JetBrains WebStorm,因为它是我发现的唯一适用于前端和后端 JavaScript 的 JavaScript IDE。

它可以在多个操作系统上运行,并且内置 Node.js 调试(以及大量其他内容)](http ://www.jetbrains.com/webstorm/features/index.html)。

我唯一的“问题”/愿望清单项目

  1. 在 Mac 上,它似乎比 Windows 上更需要资源 在版本 6 中,这似乎不再是问题。
  2. 如果它有 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 are were:

  1. It seems to be more resource hungry on Mac than Windows It no longer seems an issue in version 6.
  2. 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.
眼眸 2024-08-22 04:14:18

这里有很多很棒的答案,但我想添加我的观点(基于我的方法的演变)

调试日志

让我们面对现实,我们都喜欢一个好的 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

enter image description here

It is a painful thing
To look at your own trouble and know
That you yourself and no one else has made it

Sophocles, Ajax

情场扛把子 2024-08-22 04:14:18

Theseus 是 Adob​​e 研究的一个项目,可让您在开源编辑器中调试 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.

screenshot

拥醉 2024-08-22 04:14:18

适用于 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.

铃予 2024-08-22 04:14:18

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...

心的憧憬 2024-08-22 04:14:18

我编写了一种不同的方法来调试 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.

Enter image description here

An opensource cross-platform visual debugger.

Installation:

npm install iron-node -g;

Debug:

iron-node yourscript.js;

瑾夏年华 2024-08-22 04:14:18

我创建了一个名为 pry.js 的简洁小工具,可以帮助您。

在代码中的某个位置放置一个简单的语句,正常运行脚本,节点将停止当前线程,让您可以访问所有变量和函数。随意查看/编辑/删除它们!

var pry = require('pryjs')

class FizzBuzz

  run: ->
    for i in [1..100]
      output = ''
      eval(pry.it) // magic
      output += "Fizz" if i % 3 is 0
      output += "Buzz" if i % 5 is 0
      console.log output || i

  bar: ->
    10

fizz = new FizzBuzz()
fizz.run()

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!

var pry = require('pryjs')

class FizzBuzz

  run: ->
    for i in [1..100]
      output = ''
      eval(pry.it) // magic
      output += "Fizz" if i % 3 is 0
      output += "Buzz" if i % 5 is 0
      console.log output || i

  bar: ->
    10

fizz = new FizzBuzz()
fizz.run()
夜巴黎 2024-08-22 04:14:18

如果您使用 Atom IDE,则可以安装 node-debugger 包。

If you are using the Atom IDE, you can install the node-debugger package.

虐人心 2024-08-22 04:14:18

使用 Chrome 版本 67.0.3396.62(+)

  1. 运行节点应用程序

node --inspect-brk=0.0.0.0:9229 server.js(服务器js文件名)

  1. 在 chrome 中浏览您的应用程序,例如“localhost:port”
  2. 打开 DevTools。
  3. 单击响应设备图标旁边的节点图标。

输入图像描述这里

将会弹出另一个 DevTools 窗口,专门用于调试节点应用程序。

输入图像描述这里

Using Chrome Version 67.0.3396.62(+)

  1. Run node app

node --inspect-brk=0.0.0.0:9229 server.js(server js filename)

  1. Browse your app in chrome e.g. "localhost:port"
  2. Open DevTools.
  3. Click the the node icon beside the responsive device icon.

enter image description here

There will be another DevTools window that will pop out specifically for debugging node app.

enter image description here

乖乖哒 2024-08-22 04:14:18

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.

八巷 2024-08-22 04:14:18

我整理了一个关于使用 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.

童话 2024-08-22 04:14:18

Visual Studio Code 可以帮助我们进行调试。

Visual Studio Code will work for us in debugging.

梦情居士 2024-08-22 04:14:18

使用网络风暴!它非常适合调试 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

不交电费瞎发啥光 2024-08-22 04:14:18

如果您需要一个强大的 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.

一曲琵琶半遮面シ 2024-08-22 04:14:18

假设您的计算机上安装了 node-inspector(如果没有,只需输入“npm install -g node-inspector”),您只需运行:

node-inspector & node --debug-brk scriptFileName.js

并将命令行中的 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:

node-inspector & node --debug-brk scriptFileName.js

And paste the URI from the command line into a WebKit (Chrome / Safari) browser.

执手闯天涯 2024-08-22 04:14:18

使用 --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

分分钟 2024-08-22 04:14:18

有新的开源 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.

黄昏下泛黄的笔记 2024-08-22 04:14:18

有很多可能性...

调试支持通常使用 v8 来实现调试协议或较新的 Chrome 调试协议

There are many possibilities...

Debug support is often implemented using the v8 Debugging Protocol or the newer Chrome Debugging Protocol.

难忘№最初的完美 2024-08-22 04:14:18

IntelliJ 非常适合 Node.js。

此外,IntelliJ 很好地支持“代码帮助”。

IntelliJ works wonderfully for Node.js.

In addition, IntelliJ supports 'Code Assistance' well.

焚却相思 2024-08-22 04:14:18

NetBeans IDE 自 版本 8.1

<...>

新功能亮点

Node.js 应用程序开发

  • 新建 Node.js 项目向导
  • 新的 Node.js Express 向导
  • 增强的 JavaScript 编辑器
  • 对运行 Node.js 应用程序的新支持
  • 新支持调试 Node.js 应用程序。

<...>

其他参考:

  1. NetBeans Wiki / NewAndNoteworthyNB81
  2. NetBeans IDE、Geertjan-Oracle 中的 Node.js Express 应用

The NetBeans IDE has had Node.js support since version 8.1:

<...>

New Feature Highlights

Node.js Application Development

  • New Node.js project wizard
  • New Node.js Express wizard
  • Enhanced JavaScript Editor
  • New support for running Node.js applications
  • New support for debugging Node.js applications.

<...>

Additional references:

  1. NetBeans Wiki / NewAndNoteworthyNB81.
  2. Node.js Express App in NetBeans IDE, Geertjan-Oracle.
染柒℉ 2024-08-22 04:14:18

使用这个命令

DEBUG_LEVEL=all node file.js
DEBUG=* node file.js
node file.js --inspect

Use this commands

DEBUG_LEVEL=all node file.js
DEBUG=* node file.js
node file.js --inspect
ぺ禁宫浮华殁 2024-08-22 04:14:18

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

远昼 2024-08-22 04:14:18
node-debug -p 8888 scriptFileName.js
node-debug -p 8888 scriptFileName.js
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文