# 运行和调试 Node.js

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

> **TL;DR**
> 必需插件  :
>
>
>
> `JavaScript 和 TypeScript` 、  `JavaScript Debugger` ：插件已与 GoLand 捆绑，并默认启用。
>
>
>
> `Node.js` : install the plugin on the   设置 | 插件   page.

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

## 在开始之前

Procedure:

1. 确保  JavaScript 和 TypeScript 和  JavaScript 调试器 所需插件已在   设置 | 插件  页面上启用。 有关更多信息，请参阅 [插件](managing-plugins.html)。

2. 按照 [从 JetBrains Marketplace 安装插件](managing-plugins.html#install_plugin_from_repo) 中的说明，在   设置 | 插件   页面上安装并启用  Node.js  插件。

## 运行 Node.js 应用程序

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

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

1. 转到  `运行 | 编辑配置` 。 或者，从工具栏的   运行   小部件中选择   编辑配置  。

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

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

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

如果您选择了   项目   别名，GoLand 将自动使用   Node.js 运行时   字段中的项目默认解释器。 在大多数情况下， GoLand 会检测到项目的默认运行时，并自动填写该字段。

您还可以选择另一个已配置的本地解释器，或点击 ![“浏览”按钮](https://resources.jetbrains.com/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 特定参数。

* 要在浏览器中自动打开应用程序，请配置一个 before-launch 任务。 点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.general.arrowRight.svg) 以展开 启动前 区域，点击 ![添加按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg)，并从列表中选择 启动 Web 浏览器 。 在打开的对话框中，指定应用程序起始页的 URL，例如， `localhost:3000` 用于 Express 应用程序，并选择要使用的浏览器。 ![启动前任务：打开浏览器](https://resources.jetbrains.com/help/img/idea/2026.3/ws_node_run_configuration_open_browser_before_launch_task.png)

Procedure: 运行应用程序

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

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

![使用运行/调试配置启动应用程序](https://resources.jetbrains.com/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/help/img/idea/2026.3/app.expui.general.add.svg) 旁边的   将显示在控制台中的日志文件   字段，它列出了可用的日志文件（如果有）。

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

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

5. 可选：

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

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

## 调试 Node.js 应用程序

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

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

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

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

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

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

Procedure:

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

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

要在浏览器中自动打开应用程序，请配置一个 before-launch 任务。 点击 ![](https://resources.jetbrains.com/help/img/idea/2026.3/app.general.arrowRight.svg) 以展开   启动前   区域，点击 ![添加按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg)，并从列表中选择   启动 Web 浏览器  。 在打开的对话框中，指定应用程序起始页的 URL，例如，  `localhost:3000`  用于 Express 应用程序，并选择要使用的浏览器。

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

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

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

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

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

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

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

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

使用 GoLand，您可以调试已在开发模式下运行的应用程序，这意味着该应用程序是使用  `--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/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/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/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+`` (VSCode), `⌃ `` (VSCode (macOS))），然后输入： `node --inspect-brk <path to the starting page of your application relative to the project root>`

3. 终端  、   运行  工具窗口或者   控制台  选项卡中的   调试  工具窗口会显示一条信息消息  `Debugger listening <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` (VSCode), `Ctrl+Shift` (VSCode (macOS))并点击链接。

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

> **Note:**
> 或者，复制  `Debugger listening <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. Go 到主菜单中的  `运行 | 编辑配置` ，然后在打开的   编辑配置   对话框中点击 ![添加新配置](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.general.add.svg)，并从列表中选择   附加到 Node.js/Chrome  。

![选择“Attach to Node.js /Chrome”配置类型](https://resources.jetbrains.com/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/help/img/idea/2026.3/ws_node_debug_attach_to_app_run_config.png)

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

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

> **Warning:**
> 确保该端口与应用程序运行的端口不同。 默认端口为 9229。

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

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

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

7. 切换到 GoLand。 在   调试   工具窗口中， [逐步执行断点](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)。

内置调试器 GoLand 可以自动重新连接到运行中的 Node.js 进程。 这使您可以调试使用 [nodemon utility](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+`` (VSCode), `⌃ `` (VSCode (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 运行/调试配置传递  `inspect`  标志 [如上所述](#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/help/img/idea/2026.3/ws_run_config_attach_to_node_reconnect.png)

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

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

3. 从工具栏的   运行  小部件列表中，选择新创建的   附加到 Node.js/Chrome  配置，然后点击旁边的 ![调试按钮](https://resources.jetbrains.com/help/img/idea/2026.3/app.expui.run.debug.svg)。 调试器将在 GoLand 您的代码中设置的断点处停止。

现在，每次您对代码进行更改并保存它们 `Ctrl+S` (Windows), `⌘ S` (macOS), `⌘ S` (IntelliJ IDEA Classic (macOS)), `⌘ S` (macOS System Shortcuts), `Ctrl+S` (XWin), `Ctrl+S` (GNOME), `Ctrl+S` (KDE), `Ctrl+X, Ctrl+S` (Emacs), `Ctrl+S` (Sublime Text), `⌘ S` (Sublime Text (macOS)), `Ctrl+K, S` (VSCode), `⌘ ⌥ S` (VSCode (macOS))时，nodemon 会自动重新加载应用程序，并且调试器会自动重新附加到重新启动的进程。

