node.js 更改文件时重新启动节点

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

Restart node upon changing a file

node.js

提问by Gajus

For someone who is coming from PHP background the process of killing node and starting it again after every code change, seems very tedious. Is there any flag when starting a script with node to automatically restart node when code change is saved?

对于来自 PHP 背景的人来说,每次代码更改后杀死节点并重新启动它的过程似乎非常乏味。使用 node 启动脚本以在保存代码更改时自动重新启动 node 时是否有任何标志?

采纳答案by hyde

forever modulehas a concept of multiple node.js servers, and can start, restart, stop and list currently running servers. It can also watch for changing files and restart node as needed.

Forever 模块有多个 node.js 服务器的概念,可以启动、重启、停止和列出当前正在运行的服务器。它还可以监视更改的文件并根据需要重新启动节点。

Install it if you don't have it already:

如果您还没有安装它,请安装它:

npm install forever -g

After installing it, call the forevercommand: use the -wflag to watch file for changes:

安装后,调用forever命令:使用-w标志监视文件的更改:

forever -w ./my-script.js

In addition, you can watch directory and ignore patterns:

此外,您可以查看目录并忽略模式:

forever --watch --watchDirectory ./path/to/dir --watchIgnore *.log ./start/file

回答by almypal

A good option is Node-supervisorand Node.js Restart on File Changeis good article on how to use it, typically:

一个不错的选择是Node-supervisorNode.js Restart on File Change是关于如何使用它的好文章,通常:

 npm install supervisor -g

and after migrating to the root of your application use the following

并在迁移到应用程序的根目录后使用以下内容

 supervisor app.js

回答by Menztrual

You should look at something like nodemon.

你应该看看类似nodemon 的东西。

Nodemon will watch the files in the directory in which nodemon was started, and if they change, it will automatically restart your node application.

Nodemon 将监视启动 nodemon 的目录中的文件,如果它们发生更改,它将自动重新启动您的节点应用程序。

Example:

例子:

nodemon ./server.js localhost 8080

or simply

或者干脆

nodemon server

回答by Kay

Various NPM packages are available to make this task easy.

可以使用各种 NPM 包来简化此任务。

For Development

发展

For Production(with extended functionality such as clustering, remote deploy etc)

用于生产(具有扩展功能,例如集群、远程部署等)

Comparison between Forever, pm2,and StrongLoop can be found on StrongLoop's website.

Forever、pm2 和 StrongLoop 之间的比较可以在StrongLoop的网站上找到。

回答by Abhinav bhardwaj

You can also try nodemon

你也可以试试nodemon

To Install Nodemon

安装 Nodemon

npm install -g nodemon

To use Nodemon

使用 Nodemon

Normally we start node program like:

通常我们像这样启动节点程序:

node server.js

But here you have to do like:

但在这里你必须这样做:

nodemon server.js

回答by Adiii

node-dev

节点开发

node-dev is great alternative to both nodemon and supervisor for developers who like to get growl (or libnotify) notifications on their desktop whenever the server restartsor when there is an erroror changeoccur in file.

node-dev 是 nodemon 和 supervisor 的绝佳替代品,对于喜欢在服务器重新启动或文件发生错误更改时在桌面上收到咆哮(或 libnotify)通知的开发人员。

Installation:

安装:

npm install -g node-dev

Use node-dev, instead of node:

使用 node-dev,而不是 node:

node-dev app.js

Notification on Changing file so server start automatically

关于更改文件以便服务器自动启动的通知

enter image description here

在此处输入图片说明

console out put

控制台输出

enter image description here

在此处输入图片说明

回答by sp2danny

I use runjslike:

我使用runjs像:

runjs example.js

The package is called just run

该包被称为只是 run

npm install -g run

回答by Muhammad Ishtiaq Hussain

Follow the steps:

按照步骤:

  1. npm install --save-dev nodemon

  2. Add the following two lines to "script" section of package.json:

  1. npm install --save-dev nodemon

  2. 将以下两行添加到 package.json 的“脚本”部分:

"start": "node ./bin/www",
"devstart": "nodemon ./bin/www"

as shown below:

如下所示:

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node ./bin/www",
    "devstart": "nodemon ./bin/www"
}
  1. npm run devstart
  1. npm run devstart

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/skeleton_website