# 运行和调试 Node.js

> For the complete documentation index, see [llms.txt](https://www.jetbrains.com.cn/help/idea/llms.txt).

> **TL;DR**
> 所需插件  :
>
>
>
> `JavaScript 和 TypeScript`  -  插件与 IntelliJ IDEA 捆绑在一起，并默认启用。
>
>
>
> `JavaScript Debugger` ，  `Node.js`  -  在   设置 | 插件   页面上安装插件。 插件仅在具有 Ultimate 订阅的 IntelliJ IDEA 中可用。

IntelliJ IDEA 帮助您运行和调试您的 Node.js 应用程序。 您可以调试从 IntelliJ IDEA 启动的应用程序，也可以附加到已在运行的应用程序。

## 开始之前

Procedure:

1. 请确保已在设置中启用  JavaScript 和 TypeScript  插件。 按 `Ctrl+Alt+S` (Windows), `⌘ Comma` (macOS), `⌘ Comma` (IntelliJ IDEA Classic (macOS)), `⌘ Comma` (macOS System Shortcuts), `Ctrl+Alt+S` (XWin), `Ctrl+Alt+S` (GNOME), `Ctrl+Alt+S` (KDE), `Ctrl+Alt+S` (Emacs), `Ctrl+Alt+S` (Sublime Text), `⌘ Comma` (Sublime Text (macOS)), `Ctrl+Alt+S` (Visual Studio), `⌘ Comma` (Visual Studio (macOS)), `Ctrl+Alt+S` (NetBeans), `Ctrl+Alt+S` (Eclipse), `⌘ Comma` (Eclipse (macOS))打开设置，然后选择  `插件` 。 在搜索字段中，键入  JavaScript 和 TypeScript 。 有关插件的更多信息，请参阅 [管理插件](managing-plugins.html)。

2. 安装  Node.js  和  JavaScript 调试器  插件（位于   设置 | 插件   页面），如 [从 JetBrains Marketplace 安装插件](managing-plugins.html#install_plugin_from_repo)中所述。 插件仅在具有 Ultimate 订阅的 IntelliJ IDEA 中可用。

## 运行 Node.js 应用程序

IntelliJ IDEA 根据  Node.js  类型的运行配置运行 Node.js 应用程序。 IntelliJ IDEA 还使用此配置来 [与 Node.js 应用程序一起启动调试器](#debugLocal)。

Procedure: 创建 Node.js 运行/调试配置

1. 前往  `运行 | 编辑配置` 。 或者，在工具栏上的   运行   小部件中选择   编辑配置  。

![打开编辑配置对话框](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_run_config_create.png)

在打开的   编辑配置  对话框中，点击工具栏上的   添加  按钮（ !["添加"按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/调试配置：Node.js](run-debug-configuration-node-js.html) 对话框。

2. 指定要使用的 Node.js 运行时。

如果您选择   项目   别名， IntelliJ IDEA 将自动使用在 [JavaScript 运行时](javascript-runtime.html) 页面上   Node.js 运行时   字段中的项目默认解释器。 在大多数情况下， IntelliJ IDEA 会检测到项目的默认运行时，并自动填写该字段。

您还可以选择其他已配置的本地或远程解释器，或点击 ![浏览按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.general.ellipsis.svg) 并配置一个新的解释器。

3. 在   文件   字段中，指定启动应用程序的主文件路径（例如，  `bin/www`  对于 [Express](http://expressjs.com/) 应用程序）。

4. 可选：

* 指定自定义 Node.js 启动的 Node 参数 。 例如，您可能希望启用一个试验性的 Node.js 功能或传递其他选项。 更多信息，请参考 [Node.js official website](https://nodejs.org/api/cli.html)。

* 在 应用程序参数 字段中，指定要通过 [process.argv](http://nodejs.org/docs/latest/api/process.html#process.argv) 数组在启动时传递给应用程序的 Node.js 特定参数。

* 要在浏览器中自动打开应用程序，请配置启动前任务。 点击 ![](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.general.arrowRight.svg) 展开 启动前 区域，点击 !["添加"按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg)，然后从列表中选择 启动 Web 浏览器‍™™™™™™™‍ 。 在打开的对话框中，指定应用程序起始页的 URL，例如 Express 应用程序的 `localhost:3000` ，以及要使用的浏览器。 ![启动前任务：打开浏览器](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_run_configuration_open_browser_before_launch_task.png)

Procedure: 运行应用程序

* [按照上述描述创建运行/调试配置](#Node.js_run)或者修改现有配置，然后在对话框中点击   运行  。

* 或者，从主工具栏的列表中选择新创建的运行配置，然后点击 ![运行按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.run.run.svg)。

![使用运行/调试配置启动应用程序](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_run_app_with_rc.png)

应用程序启动后， [运行工具窗口](run-tool-window.html)会打开并显示应用程序的输出。

如果您在应用程序中使用像 [morgan](https://www.npmjs.com/package/morgan) 这样的日志工具并且该工具将日志写入文件，您可以在   控制台   选项卡中的   运行   工具窗口中查看这些日志。

Procedure: 在运行 Node.js 应用程序时管理日志

1. 创建一个 Node.js 运行/调试配置 [如上所述](#Node.js_run)，然后转到   日志  选项卡。

2. 点击 !["添加"按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg) 旁边的   将显示在控制台中的日志文件   字段，它列出了可用的日志文件（如果有）。

3. 在打开的   编辑日志文件别名  对话框中，输入要在日志条目列表中显示的别名，并指定日志文件的位置。 选择是否显示此模式涵盖的所有文件或仅显示最后一个文件。

4. 点击   OK   以返回   Node.js 运行/调试配置   对话框，新日志文件已添加到列表中。 请选中旁边的   处于活动状态   复选框。 若要跳过先前的内容，请选中   跳过内容   复选框。

5. 可选：

* 要启用将 进程控制台 输出保存到日志文件，请选中 将控制台输出保存到文件 复选框并指定文件位置。

* 选择您想要显示的 进程控制台 的时间。

## 调试 Node.js 应用程序

IntelliJ IDEA 使调试 Node.js 应用程序更简单。 您可以直接在您的 JavaScript 或 TypeScript 代码中设置断点，这样就不再需要任何  `debugger`  和  `console.log()`  语句了。 您可以通过许多方法来帮助您探索代码并了解错误所在。 在   调试  工具窗口中，您可以查看调用堆栈和当前状态的变量，在编辑器中评估表达式，并逐步调试代码。

您可以通过两种方式启动调试会话：

* 使用 Node.js 运行/调试配置启动调试器与您的应用程序一起运行。

* 将调试器附加到已运行的应用程序。 在这种情况下，您的应用程序已经在 [debug mode](https://nodejs.org/en/docs/guides/debugging-getting-started/)中运行，IntelliJ IDEA会附加到正在运行的进程。 IntelliJ IDEA 识别 `--inspect` 、 `--inspect-brk` 和现在不推荐使用的 `--debug` 标志，因此您可以使任何应用程序可调试。 要调试正在运行的应用程序，请使用 Attach to Node.js/Chrome 配置。

使用 IntelliJ IDEA，您还可以调试在 [Vagrant boxes](vagrant-support.html)、 [Docker containers](docker.html) 中运行的 Node.js 应用程序，或者通过 [various transfer protocols](configuring-synchronization-with-a-remote-host.html) 或 SSH 访问的远程主机上的 Node.js 应用程序。

### 在您的计算机上与 Node.js 应用程序一起启动调试器

![启动和调试 Node.js Express 应用](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_debug-local.animated.gif)

Procedure:

1. 在代码中必要的位置设置 [断点](using-breakpoints.html)。

2. 创建一个 Node.js 运行/调试配置 [如上所述](#Node.js_run)。

要在浏览器中自动打开应用程序，请配置启动前任务。 点击 ![](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.general.arrowRight.svg) 展开   启动前   区域，点击 !["添加"按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg)，然后从列表中选择   启动 Web 浏览器‍™™™™™™™‍  。 在打开的对话框中，指定应用程序起始页的 URL，例如 Express 应用程序的  `localhost:3000` ，以及要使用的浏览器。

![启动前任务：打开浏览器](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_run_configuration_open_browser_before_launch_task.png)

> **Tip:**
> 如果需要，IntelliJ IDEA 可以生成一个  JavaScript Debug  配置，并与  Node.js  配置一起自动启动，如 [调试服务器端和客户端代码](node-debug-server-client.html) 中所述。

3. 在工具栏的   运行   小部件列表中，选择新创建的   Node.js   配置，然后点击其旁边的 ![调试按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.run.debug.svg)。

![使用运行/调试配置启动 Node.js 调试会话](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_debug_app_with_rc.png)

[调试工具窗口](debug-tool-window.html)打开。

4. 执行会触发断点所在代码执行的步骤。 例如，在浏览器中从应用程序的起始页面导航到另一个页面。

5. 切换到 IntelliJ IDEA，此时   调试   工具窗口的控件已启用。 继续调试会话—— [逐步执行断点](stepping-through-the-program.html)，在帧之间切换，动态更改值， [检查被挂起的程序](examining-suspended-program.html)， [评估表达式](examining-suspended-program.html#evaluating-expressions)，以及 [设置监视](examining-suspended-program.html#watches)。

### 调试正在运行的 Node.js 应用程序

使用 IntelliJ IDEA，您可以调试已在开发模式下运行的应用程序，这意味着该应用程序是使用  `--inspect`  或  `--inspect -brk`  标志启动的。 调试会话通过  Attach to Node.js/Chrome 配置启动，并使用 [Chrome Debugging Protocol](https://nodejs.org/api/debugger.html#debugger_v8_inspector_integration_for_node_js)。

#### 从"运行"或"调试"工具窗口或内置终端启动调试器

如果使用  `--inspect`  或  `--inspect-brk`  标志启动应用程序，您可以从内置的   终端  、   运行   工具窗口或   调试   工具窗口启动调试会话。

![从运行工具窗口将调试器附加到正在运行的应用程序](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_debug_attach_from_run_tw.animated.gif)

Procedure:

1. 请根据需要设置 [断点](using-breakpoints.html)。

2. 运行您的应用程序，并使用  `--inspect` 或  `--inspect-brk` 标志。 您可以通过多种方式实现，例如：

* 在您的 `package.json` 中，点击脚本旁边带有 `--inspect` 或 `--inspect -brk` 标志的边栏中的 ![运行按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app-client.expui.run.run.svg)并选择 运行 '<script name>' 。 有关详细信息，请参阅 [运行脚本](installing-and-removing-external-software-using-node-package-manager.html#ws_npm_running_npm_scripts)。 ![在调试模式下启动应用程序：启动脚本](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_attach_debugger_run_npm_script_select_action.png)

* 打开命令行 shell 或嵌入式 终端 （ `Alt+F12` (Windows), `⌥ F12` (macOS), `⌥ F12` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ T` (macOS System Shortcuts), `Alt+F12` (XWin), `Alt+F12` (GNOME), `Alt+F12` (KDE), `Alt+F12` (Emacs), `Alt+F12` (Sublime Text), `⌥ F12` (Sublime Text (macOS)), `Ctrl+Alt+1` (Visual Studio), `⌘ ⌃ 1` (Visual Studio (macOS)), `Alt+F12` (NetBeans), `Alt+F12` (Eclipse), `⌥ F12` (Eclipse (macOS))），然后输入： `node --inspect-brk <path to the starting page of your application relative to the project root>`

3. 终端  、   运行  工具窗口或者   控制台  选项卡中的   调试  工具窗口会显示一条信息消息  `调试器正在监听 <host>:<port>` ，默认端口是  `9229` 。 要开始调试，请按住 `Ctrl+Shift` (Windows), `Ctrl+Shift` (macOS), `Ctrl+Shift` (IntelliJ IDEA Classic (macOS)), `Ctrl+Shift` (macOS System Shortcuts), `Ctrl+Shift` (XWin), `Ctrl+Shift` (GNOME), `Ctrl+Shift` (KDE), `Ctrl+Shift` (Emacs), `Ctrl+Shift` (Sublime Text), `Ctrl+Shift` (Sublime Text (macOS)), `Ctrl+Shift` (Visual Studio), `Ctrl+Shift` (Visual Studio (macOS)), `Ctrl+Shift` (NetBeans), `Ctrl+Shift` (Eclipse), `Ctrl+Shift` (Eclipse (macOS))并点击链接。

IntelliJ IDEA 启动调试会话，并自动生成  Attach to Node.js/Chrome 配置。 执行会触发断点所在代码执行的步骤。 例如，在浏览器中从应用程序的起始页面导航到另一个页面。

> **Note:**
> 或者，复制  `调试器正在监听 <host>:<port>`  消息中的端口号，稍后粘贴到 [Attach to Node.js/Chrome configuration](#ws_debug_node_remote_inspect_mappings)。

#### 通过运行/调试配置将调试器附加到正在运行的应用程序

Procedure:

1. 运行您的应用程序，并使用  `--inspect`  或  `--inspect-brk`  标志， [如上所述](#ws_node_run_with_inspect)。

2. 请根据需要设置 [断点](using-breakpoints.html)。

3. 在主菜单中转到  `运行 | 编辑配置` ，然后在打开的   编辑配置   对话框中点击 ![添加新配置](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.general.add.svg)，并从列表中选择   附加到 Node.js/Chrome  。

![选择“Attach to Node.js /Chrome”配置类型交付情況：已完成。                                                ](https://resources.jetbrains.com.cn/help/img/idea/2026.3/Chrome](run-debug-configuration-node-js-remote-debug.html) 对话框。

4. 指定目标应用程序运行的主机和启动 Node.js 进程时传递给  `--inspect`  或  `--inspect-brk`  的端口以进行连接。

![附加到 Node/Chrome 运行/调试配置](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_debug_attach_to_app_run_config.png)

请从信息消息中复制端口号  `调试器正在监听 <host>:<port>` ，在   终端  工具窗口或 [运行工具窗口](run-tool-window.html)，它控制正在运行的应用程序。

![从 Run 工具窗口或 Terminal 开始调试：信息消息](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_node_debug_attach_from_terminal_info_message.png)

> **Warning:**
> 请确保此端口不同于应用程序运行所用的端口。 默认端口为 9229。

> **Tip:**
> 可选：在   本地文件的远程 URL   区域指定项目文件夹的远程路径。 如果正在运行的应用程序根文件夹与您的 IntelliJ IDEA 项目根文件夹名称不同，这将非常有用。

5. 在工具栏的   运行   小部件列表中，选择新创建的   附加到 Node.js/Chrome   配置，然后点击其旁边的 ![调试按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/app.expui.run.debug.svg)。 [调试工具窗口](debug-tool-window.html) 打开。

6. 执行将触发断点代码的操作。 控制调试会话的权力回到 IntelliJ IDEA。

7. 切换到 IntelliJ IDEA。 在   调试   工具窗口中， [逐步执行断点](stepping-through-the-program.html)，切换帧，动态更改值， [检查挂起的程序](examining-suspended-program.html)， [评估表达式](examining-suspended-program.html#evaluating-expressions)，并 [设置监视](examining-suspended-program.html#watches)。

### 调试使用 nodemon 的 Node.js 应用程序

> **Tip:**
> 如需更多信息，请参阅 [运行和调试脚本](installing-and-removing-external-software-using-node-package-manager.html#ws_npm_running_npm_scripts)。

内置调试器 IntelliJ IDEA 可以自动重新连接到运行中的 Node.js 进程。 这样，您可以调试使用 [nodemon 实用程序](https://nodemon.io/) 的 Node.js 应用程序，该实用程序会在代码更新时自动重新加载 Node.js 进程。

要调试此类应用程序，您需要在  debug 模式下启动它（带有  `--inspect`  或  `--inspect-brk`  标志），然后使用 [Attach to a Node.js/Chrome](#node_debugging_overview)调试配置并启用   自动重新连接  选项来连接它。

Procedure: 安装 nodemon

* 在命令行 shell 或嵌入式   终端  （ `Alt+F12` (Windows), `⌥ F12` (macOS), `⌥ F12` (IntelliJ IDEA Classic (macOS)), `⌘ ⌃ T` (macOS System Shortcuts), `Alt+F12` (XWin), `Alt+F12` (GNOME), `Alt+F12` (KDE), `Alt+F12` (Emacs), `Alt+F12` (Sublime Text), `⌥ F12` (Sublime Text (macOS)), `Ctrl+Alt+1` (Visual Studio), `⌘ ⌃ 1` (Visual Studio (macOS)), `Alt+F12` (NetBeans), `Alt+F12` (Eclipse), `⌥ F12` (Eclipse (macOS))）中，输入  `npm install --save-dev nodemon`  或  `yarn add nodemon --dev` ，以开发依赖身份安装 nodemon。

Procedure: 在调试模式下使用 nodemon 启动应用程序

* 创建并运行以下  `npm debug` 脚本：

```SHELL
"debug": "nodemon --inspect <path_to_the_file_that_starts_your_application>

```

如需更多信息，请参阅 [运行和调试脚本](installing-and-removing-external-software-using-node-package-manager.html#ws_npm_running_npm_scripts)。

* 或者，通过 Node.js 运行/调试配置传递  `检查`  标志， [如上所述](#ws_node_run_config_optionally)。

Procedure: 调试应用程序

1. 请根据需要在代码中设置 [断点](using-breakpoints.html)。

2. 创建一个新的   附加到 Node.js/Chrome  配置，如 [调试正在运行的 Node.js 应用程序](#node_debugging_overview)中所述，并选择   自动重新连接  复选框。

![不附加 Node.js 运行配置：选中“自动重新连接”复选框](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_run_config_attach_to_node_reconnect.png)

通常，您不需要更改配置中的端口  `9229` ，因为这是调试器正在监听的默认端口。 但是，当您在调试模式下运行应用程序时，您可以仔细检查记录消息中使用的端口。

![使用 nodemon 以调试模式运行的 Node.js 应用程序：检查端口](https://resources.jetbrains.com.cn/help/img/idea/2026.3/ws_debug_port_nodemon.png)

3. 在工具栏的   运行   小部件列表中，选择新创建的   附加到 Node.js/Chrome   配置，然后点击其旁边的 ![调试按钮](https://resources.jetbrains.com.cn/help/img/idea/2026.3/Chrome](run-debug-configuration-node-js-remote-debug.html) [运行工具窗口](run-tool-window.html)

