当前位置: 首页 > 工具软件 > nodemon > 使用案例 >

node nodemon_如何使用nodemon自动重启Node.js应用

胡霖
2023-12-01

node nodemon

介绍 (Introduction)

In Node.js, you need to restart the process to make changes take effect. This adds an extra step to your workflow to have the changes take place. You can eliminate this extra step by using nodemon to restart the process automatically.

在Node.js中,您需要重新启动过程以使更改生效。 这为工作流程增加了一个额外的步骤,以使更改生效。 您可以通过使用nodemon自动重新启动该过程来消除此额外步骤。

nodemon is a command-line interface (CLI) utility developed by @rem that wraps your Node app, watches the file system, and automatically restarts the process.

nodemon是由@rem开发的命令行界面(CLI)实用程序,用于包装您的Node应用程序, nodemon文件系统并自动重新启动该过程。

In this article, you will learn about installing, setting up, and configuring nodemon.

在本文中,您将学习有关安装,设置和配置nodemon

先决条件 (Prerequisites)

If you would like to follow along with this article, you will need:

如果您想继续阅读本文,则需要:

第1步-安装nodemon (Step 1 — Installing nodemon)

First, you will need to install nodemon on your machine. Install the utility either globally or locally on your project using npm or Yarn:

首先,您将需要在nodemon上安装nodemon 。 使用npm或Yarn在您的项目中全局或本地安装实用程序:

全局安装 (Global Installation)

You can install nodemon globally with npm:

您可以使用npm全局安装nodemon

  • npm install nodemon -g

    npm安装nodemon -g

Or with Yarn:

或使用纱线:

  • yarn global add nodemon

    纱线全局添加节点

本地安装 (Local Installation)

You can also install nodemon locally with npm. When performing a local installation, we can install nodemon as a dev dependency with --save-dev (or --dev):

您也可以使用npm在本地安装nodemon 。 执行本地安装时,我们可以使用--save-dev (或--dev )将nodemon安装为dev依赖 --dev

  • npm install nodemon --save-dev

    npm install nodemon --save-dev

Or with Yarn:

或使用纱线:

  • yarn add nodemon --dev

    纱线添加nodemon --dev

One thing to be aware of with a local install is you won’t be able to use the nodemon command directly from the command line:

本地安装要注意的一件事是,您将无法直接从命令行使用nodemon命令:

Output
输出量
  • command not found: nodemon

    找不到命令:nodemon

However, you can use it as part of some npm scripts or with npx.

但是,您可以将其用作某些npm脚本的一部分或与npx一起使用

This concludes the nodemon installation process. Next, we will use nodemon with our projects.

到此结束nodemon安装过程。 接下来,我们将在项目中使用nodemon

第2步—使用nodemon设置示例Express项目 (Step 2 — Setting Up an Example Express Project with nodemon)

We can use nodemon to start a Node script. For example, if we have an Express server setup in a server.js file, we can start it and watch for changes like this:

我们可以使用nodemon启动Node脚本。 例如,如果我们在server.js文件中有一个Express服务器设置 ,则可以启动它并注意以下更改:

  • nodemon server.js

    nodemon server.js

You can pass in arguments the same way as if you were running the script with Node:

您可以通过与使用Node运行脚本相同的方式传递参数:

  • nodemon server.js 3006

    nodemon server.js 3006

Every time you make a change to a file with one of the default watched extensions (.js, .mjs, .json, .coffee, or .litcoffee) in the current directory or a subdirectory, the process will restart.

您更改与默认观看扩展(的一个文件每次.js.mjs.json.coffee ,或.litcoffee )在当前目录或子目录,该进程将重新启动。

Let’s assume we write an example server.js file that outputs the message: Dolphin app listening on port ${port}!.

假设我们编写了一个示例server.js文件,该文件输出以下消息: Dolphin app listening on port ${port}!

We can run the example with nodemon:

我们可以使用nodemon运行示例:

  • nodemon server.js

    nodemon server.js

We see the following terminal output:

我们看到以下终端输出:


   
   
Output
[nodemon] 1.17.3 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `node server.js` Dolphin app listening on port 3000!

While nodemon is still running, let’s make a change to the server.js file to output the message: Shark app listening on port ${port}!.

nodemon仍在运行时,让我们对server.js文件进行更改以输出以下消息: Shark app listening on port ${port}!

We see the following additional terminal output:

我们看到以下附加终端输出:


   
   
Output
[nodemon] restarting due to changes... [nodemon] starting `node server.js` Shark app listening on port 3000!

The terminal output from our Node.js app is displaying as expected. You can restart the process at any time by typing rs and hitting ENTER.

Node.js应用程序的终端输出将按预期显示。 您可以随时输入rs ENTER来重新启动该过程。

Alternatively, nodemon will also look for a main file specified in your project’s package.json file:

另外, nodemon还将在项目的package.json文件中查找指定的main文件:

package.json
package.json
{
  // ...
  "main": "server.js",
  // ...
}

Or, a start script:

或者,一个start脚本:

package.json
package.json
{
  // ...
  "scripts": {
    "start": "node server.js"
  },
  // ...
}

Once you make the changes to package.json, you can then call nodemon to start the example app in watch mode without having to pass in server.js.

package.json进行更改后,您可以调用nodemon以监视模式启动示例应用程序,而无需传入server.js

步骤3 —使用选项 (Step 3 — Using Options)

You can modify the configuration settings available to nodemon.

您可以修改可用于nodemon的配置设置。

Let’s go over a few of the main options:

让我们来看一些主要选项:

  • --exec: Use the --exec switch to specify a binary to execute the file with. For example, when combined with the ts-node binary, --exec can become useful to watch for changes and run TypeScript files.

    --exec :使用--exec开关指定执行文件的二进制文件。 例如,当与ts-node二进制文件结合使用时,-- --exec可以用于监视更改和运行TypeScript文件。

  • --ext: Specify different file extensions to watch. For this switch, provide a comma-separated list of file extensions (e.g., --ext js,ts).

    --ext :指定要观看的其他文件扩展名。 对于此开关,请提供以逗号分隔的文件扩展名列表(例如--ext js,ts )。

  • --delay: By default, nodemon waits for one second to restart the process when a file changes, but with the --delay switch, you can specify a different delay. For example, nodemon --delay 3.2 for a 3.2-second delay.

    --delay :默认情况下,当文件更改时, nodemon等待一秒钟以重新启动该过程,但是使用--delay开关,您可以指定其他延迟。 例如, nodemon --delay 3.2表示3.2秒的延迟。

  • --watch: Use the --watch switch to specify multiple directories or files to watch. Add one --watch switch for each directory you want to watch. By default, the current directory and its subdirectories are watched, so with --watch you can narrow that to only specific subdirectories or files.

    --watch :使用--watch开关指定要监视的多个目录或文件。 为要观看的每个目录添加一个--watch开关。 默认情况下,将监视当前目录及其子目录,因此使用--watch可以将其范围缩小到特定的子目录或文件。

  • --ignore: Use the --ignore switch to ignore certain files, file patterns, or directories.

    --ignore :使用--ignore开关可忽略某些文件,文件模式或目录。

  • --verbose: A more verbose output with information about what file(s) changed to trigger a restart.

    --verbose :更详细的输出,其中包含有关更改了哪些文件以触发重新启动的信息。

You can view all the available options with the following command:

您可以使用以下命令查看所有可用选项:

  • nodemon --help

    nodemon-帮助

Using these options, let’s create the command to satisfy the following scenario:

使用这些选项,让我们创建命令来满足以下情况:

  • watching the server directory

    查看server目录

  • specifying files with a .ts extension

    指定扩展名为.ts文件

  • ignoring files with a .test.ts suffix

    忽略带有.test.ts后缀的文件

  • executing the file (server/server.ts) with ts-node

    使用ts-node执行文件( server/server.ts )

  • waiting for three seconds to restart after a file changes

    文件更改后等待三秒钟重新启动
  • nodemon --watch server --ext ts --exec ts-node --ignore '*.test.ts' --delay 3 server/server.ts

    nodemon --watch 服务器 --ext ts --exec ts-node --ignore '* .test.ts'-- delay 3 server / server.ts

This command combines --watch, --ext, --exec, --ignore, and --delay options to satisfy the conditions for our scenario.

这个命令把--watch--ext--exec--ignore ,并--delay选项,以满足我们的方案的条件。

第4步-使用配置 (Step 4 — Using Configurations)

In the previous example, adding configuration switches when running nodemon can get quite tedious. A better solution for projects that need specific configurations is to specify these configs in a nodemon.json file.

在前面的示例中,在运行nodemon时添加配置开关可能会非常繁琐。 对于需要特定配置的项目,更好的解决方案是在nodemon.json文件中指定这些配置。

For example, here are the same configurations as the previous command line example, but placed in a nodemon.json file:

例如,这是与前面的命令行示例相同的配置,但是放置在nodemon.json文件中:

nodemon.json
nodemon.json
{
  "watch": ["server"],
  "ext": "ts",
  "ignore": ["*.test.ts"],
  "delay": "3",
  "execMap": {
    "ts": "ts-node"
  }
}

Note the use of execMap instead of the --exec switch. execMap allows you to specify binaries that should be used given certain file extensions.

请注意使用execMap而不是--exec开关。 execMap允许您指定在某些文件扩展execMap应使用的二进制文件。

Alternatively, if you’d rather not add a nodemon.json config file to your project, you can add these configurations to the package.json file under a nodemonConfig key:

另外,如果您不想将nodemon.json配置文件添加到项目中,则可以在nodemonConfig键下将这些配置添加到package.json文件中:

package.json
package.json
{
  "name": "test-nodemon",
  "version": "1.0.0",
  "description": "",
  "nodemonConfig": {
    "watch": [
      "server"
    ],
    "ext": "ts",
    "ignore": [
      "*.test.ts"
    ],
    "delay": "3",
    "execMap": {
      "ts": "ts-node"
    }
  },
  // ...

Once you make the changes to either nodemon.json or package.json, you can then start nodemon with the desired script:

一旦对nodemon.jsonpackage.json进行了更改,就可以使用所需的脚本启动nodemon

  • nodemon server/server.ts

    nodemon服务器/server.ts

nodemon will pick up the configurations and use them. This way, your configurations can be saved, shared, and repeated to avoid copy-and-pasting or typing errors in the command line.

nodemon将选择并使用配置。 这样,可以保存,共享和重复您的配置,以避免在命令行中进行复制粘贴或输入错误。

结论 (Conclusion)

In this article, you explored how to use nodemon with your Node.js applications. This tool helps automate the process of stopping and starting a Node server to view the changes.

在本文中,您探索了如何在Node.js应用程序中使用nodemon 。 该工具有助于自动化停止和启动节点服务器以查看更改的过程。

For more information about the available features and troubleshooting errors, consult the official documentation.

有关可用功能和故障排除错误的更多信息,请参阅官方文档

If you’d like to learn more about Node.js, check out our Node.js topic page for exercises and programming projects.

如果您想了解有关Node.js的更多信息,请查看我们的Node.js主题页面,以进行练习和编程项目。

翻译自: https://www.digitalocean.com/community/tutorials/workflow-nodemon

node nodemon

 类似资料: