node.js 如何使用断点和所有内容调试节点 js 应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11611162/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:03:59  来源:igfitidea点击:

How to debug node js app with breakpoints and everything?

debuggingnode.jsv8node-inspector

提问by jayarjo

I've installed node-inspector just to find out that it doesn't support breakpoints :| What's the point in it at all, bearing in mind that on big part node code is asynchronous and you simply cannot follow it step by step?..

我安装了节点检查器只是为了发现它不支持断点:| 这有什么意义,记住大部分节点代码是异步的,你根本无法一步一步地遵循它?...

I'm definitely missing a point here...

我在这里肯定错过了一点......

Anyway to debug node code with breakpoints and everything?

无论如何要使用断点和所有内容调试节点代码?

采纳答案by Yuci

(For Node 8 and later)

(适用于节点 8 及更高版本)

Node.js has a built-in debugger. Normally you can turn on the debugger in two ways:

Node.js 有一个内置的调试器。通常,您可以通过两种方式打开调试器:

  1. Start your Node.js app or script with the --inspector --inspect-brkswitch. For example:

    $ node.js --inspect index.js

  1. 使用--inspect--inspect-brk开关启动您的 Node.js 应用程序或脚本。例如:

    $ node.js --inspect index.js

(Note: --inspect-brkbreaks before user code starts)

(注意:--inspect-brk在用户代码开始之前中断)

  1. If for some reason you cannot start your Node.js app or script with the --inspectswitch, you can still instruct the Node.js process to start listening for debugging messages by signalling it with SIGUSR1 (on Linux and OS X). For Node 8 and later it will activate the Inspector API, same as the --inspect switch

    $ kill -sigusr1 23485

  1. 如果由于某种原因您无法使用--inspect开关启动 Node.js 应用程序或脚本,您仍然可以通过使用 SIGUSR1(在 Linux 和 OS X 上)发出信号来指示 Node.js 进程开始侦听调试消息。对于 Node 8 及更高版本,它将激活 Inspector API,与 --inspect 开关相同

    $ kill -sigusr1 23485

(Note: you need to replace 23485 with your own Node.js process ID)

(注意:23485 需要替换为自己的 Node.js 进程 ID)

With the debugger turned on, you can open the Google Chrome browser, and type in the address bar chrome://inspect

打开调试器后,您可以打开 Google Chrome 浏览器,然后在地址栏中输入 chrome://inspect

Then you should see an entry listed under "Remote Target". Go ahead and click "inspect".

然后您应该会看到在“远程目标”下列出的条目。继续并单击“检查”。

Now you can set breakpoints and start debugging your code.

现在您可以设置断点并开始调试您的代码。

Reference:

参考:

  1. https://nodejs.org/en/docs/guides/debugging-getting-started/
  2. Related issue on stackoverflow: Chrome Devtools Dedicated Node.js Inspector not stopping at breakpoints
  1. https://nodejs.org/en/docs/guides/debugging-getting-started/
  2. stackoverflow 上的相关问题: Chrome Devtools Dedicated Node.js Inspector not Stops at breakpoints

回答by rdrey

yupp, I've successfully used node-inspector. If you want permanentbreakpoints, simply insert debugger;in your code. See http://nodejs.org/api/debugger.html.

是的,我已经成功使用了节点检查器。如果您想要永久断点,只需debugger;在您的代码中插入即可。请参阅http://nodejs.org/api/debugger.html

Making node wait until a debugger is attached, using node --inspect-brk script.js(previously node --debug-brk script.js), can also be very helpful.

使用node --inspect-brk script.js(以前的node --debug-brk script.js)使节点等待直到连接调试器也非常有帮助。

回答by Shekar Mania

To Debug a Node js application, one can use the debug inbuilt module

要调试 Node js 应用程序,可以使用 debug 内置模块

Insert debugger;statement where you want to insert breakpoints

debugger;在要插入断点的地方插入语句

Run the file with command node inspect index.js

使用命令运行文件 node inspect index.js

Press C to continue to next breakpoint

按C继续下一个断点

you can even debug values associated to variables at that breakpoint by typing repl

您甚至可以通过键入来调试与该断点处的变量关联的值 repl

For more information, Please Refer https://nodejs.org/api/debugger.html

更多信息请参考https://nodejs.org/api/debugger.html

回答by Alvaro

Have you tried using nodemonlibrary? it can be found here.

您是否尝试过使用nodemon库?可以在这里找到

For development purposes you could start the app running nodemon. I have this script:

出于开发目的,您可以启动运行 nodemon 的应用程序。我有这个脚本:

"dev": "nodemon --inspect src/index.js"

"dev": "nodemon --inspect src/index.js"

It will break any time a debuggerstatement is reached in the code. To open the console where you can see the server code, open the console in chrome and click on the nodejs icon:enter image description here

任何时候debugger在代码中到达语句时它都会中断。要打开可以看到服务器代码的控制台,请在 chrome 中打开控制台并单击 nodejs 图标:在此处输入图片说明

It also helps you refreshing the server every time you save any file on the server.

每次在服务器上保存任何文件时,它还可以帮助您刷新服务器。

Let me know if it works!

让我知道它是否有效!

回答by bsiddiqui

Just to elaborate a bit here:

只是在这里详细说明一下:

Set a debugger wherever you want the breakpoints to be and then run your code with node debug script.js/index.js

在您希望断点所在的任何位置设置调试器,然后使用 node debug script.js/index.js 运行您的代码

When the debugger stops at you breakpoint, you will need to repl to inspect the variables.

当调试器在您的断点处停止时,您将需要 repl 来检查变量。