node nodemon
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
。
If you would like to follow along with this article, you will need:
如果您想继续阅读本文,则需要:
Node.js installed locally, which you can do by following How to Install Node.js and Create a Local Development Environment.
Node.js在本地安装,您可以按照如何安装Node.js和创建本地开发环境进行操作 。
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在您的项目中全局或本地安装实用程序:
You can install nodemon
globally with npm
:
您可以使用npm
全局安装nodemon
:
Or with Yarn:
或使用纱线:
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
:
Or with Yarn:
或使用纱线:
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
命令:
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
。
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
文件:
{
// ...
"main": "server.js",
// ...
}
Or, a start
script:
或者,一个start
脚本:
{
// ...
"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
。
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:
您可以使用以下命令查看所有可用选项:
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
)
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
选项,以满足我们的方案的条件。
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
文件中:
{
"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
文件中:
{
"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.json
或package.json
进行了更改,就可以使用所需的脚本启动nodemon
:
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
将选择并使用配置。 这样,可以保存,共享和重复您的配置,以避免在命令行中进行复制粘贴或输入错误。
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