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
Restart node upon changing a file
提问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-supervisor和Node.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
发展
- nodemon: most popular and actively maintained
- forever: second-most popular
- node-dev: no commits since May 2019
- supervisor: - no longer maintained
For Production(with extended functionality such as clustering, remote deploy etc)
用于生产(具有扩展功能,例如集群、远程部署等)
- pm2:
npm install -g pm2 - Strong Loop Process Manager:
npm install -g strongloop
Comparison between Forever, pm2,and StrongLoop can be found on StrongLoop's website.
Forever、pm2 和 StrongLoop 之间的比较可以在StrongLoop的网站上找到。
回答by Abhinav bhardwaj
回答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
关于更改文件以便服务器自动启动的通知
console out put
控制台输出
回答by sp2danny
回答by Muhammad Ishtiaq Hussain
Follow the steps:
按照步骤:
npm install --save-dev nodemonAdd the following two lines to "script" section of package.json:
npm install --save-dev nodemon将以下两行添加到 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"
}
npm run devstart
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


