javascript 使用pm2时如何自动重载Node.js项目

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

How to automatically reload Node.js project when using pm2

javascriptnode.jsexpresspm2

提问by Mr.Dung

I am currently programming Node.js with Express.js, and every time I change a line of code in the file router or app, I need to type the command:

我目前正在使用 Express.js 编写 Node.js,每次更改文件路由器或应用程序中的一行代码时,都需要键入以下命令:

pm2 reload id_project.

How do I make pm2 auto-reload the project when a file is changed?

如何在文件更改时让 pm2 自动重新加载项目?

采纳答案by Thatkookooguy

By default, node doesn't automatically refresh our server every time we change files. I don't know about specific pm2 solution, but you can do that with nodemon. Just install with: npm install -g nodemonand use with: nodemon server.js.

默认情况下,节点不会在我们每次更改文件时自动刷新我们的服务器。我不知道具体的 pm2 解决方案,但您可以使用 nodemon 来做到这一点。只需安装:npm install -g nodemon并使用:nodemon server.js

Personally, it looks like @rogier-spieker answer should get the accepted answer. (I can't even delete this answer as long as it is the accepted answer)

就个人而言,看起来@rogier-spieker 的答案应该得到公认的答案。(我什至不能删除这个答案,只要它是公认的答案)

回答by Rogier Spieker

You need to start your pm2project with the --watchoption:

您需要pm2使用以下--watch选项启动您的项目:

pm2 start <script|name|id> --watch

Where <script|name|id>refers to:

其中<script|name|id>指:

  • scriptthe path to the scriptyou want to let pm2 handle
  • namethe name of the configuration in the "ecosystem" file
  • idrefers to an already running application using pm2, which can be obtained using pm2 list(note that this would actually require a restartinstead of start, so it's probably the least desirable of the options)
  • script要让 pm2 处理的脚本的路径
  • name“生态系统”文件中的配置名称
  • id指的是一个已经使用 pm2 运行的应用程序,可以使用它来获得pm2 list(注意,这实际上需要一个restart而不是start,所以它可能是最不可取的选项)

You could also specify which files/directories to ignore:

您还可以指定要忽略的文件/目录:

pm2 start <script> --watch --ignore-watch "node_modules"

Watch & Restart

观看和重启

Or create an "ecosystem" json file describing how you want pm2to treat your project:

或者创建一个“生态系统”json 文件,描述您希望pm2如何处理您的项目:

{
  "name": "project_name",
  "script": "index.js",
  "watch": true,
  "ignore_watch": ["node_modules"]
}

JSON options

JSON 选项

回答by hthserhs

PM2 comes with a handy development toolthat allow you to start an application and restart it on file change:

PM2 带有一个方便的开发工具,允许您启动应用程序并在文件更改时重新启动它:

# Start your application in development mode
# it print the logs and restart on file change too

# Two way of running your application :
pm2-dev start my-app.js

# or

pm2-dev my-app.js

回答by Abhijeet Navgire

pm2 is a Node process manager that has lots of bells and whistles. you can run the below command to automatically restarting the node application when file changes in the directory are detected.

pm2 是一个 Node 进程管理器,有很多花里胡哨的东西。当检测到目录中的文件更改时,您可以运行以下命令自动重新启动节点应用程序。

pm2 start index.js --watch

Note that because pm2 runs things in the background, you can't just ctrl+cyour way out of a running pm2 process. You have to stop it by passing the ID or the name.

请注意,因为 pm2 在后台运行,所以您不能只是ctrl+c退出正在运行的 pm2 进程。您必须通过传递 ID 或名称来阻止它。

pm2 stop 0
pm2 stop index

other two options are below

其他两个选项如下

npx supervisor index.js
nodemon index.js