How to automatically reload Node.js project when using pm2
Asked Answered
A

4

25

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:

pm2 reload id_project.

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

Archine answered 21/4, 2015 at 8:24 Comment(1)
pm2 is a tool used in production mode. Have a look at nodemon.io , it does exactly what you want to do.Tenedos
S
5

By default, pm2 doesn’t automatically refresh our server every time we change files. You need to start your pm2 project with the --watch cli argument in order to tell pm2 to refresh when files have changed:

pm2 start id_project --watch

check out the docs for more details, or @rogier-spieker answer which is more detailed.

Swaney answered 21/4, 2015 at 8:33 Comment(4)
ok, so... my server.js uses `#!/usr/local/bin/nodemon" yet pm2 doesn't seem to invoke nodemonSpectacled
sorry, @Michael. like I said, I don't actually use pm2 so I can't help any further than I already did. I just said what I said since it sounds like it will solve the OP's problem. Try looking at other answers in this thread. maybe they can help you out.Swaney
Personally, it looks like the other answer in this thread should get the accepted answer. I can't even delete this answer as long as it is the accepted answer.Swaney
@Swaney I don't want to downvote, I think Mr.Dung should change the accepted answer... although I don't expect he will, as almost 6 years have passedMcelhaney
A
116

You need to start your pm2 project with the --watch option:

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

Where <script|name|id> refers to:

  • script the path to the script you want to let pm2 handle
  • name the name of the configuration in the "ecosystem" file
  • id refers to an already running application using pm2, which can be obtained using pm2 list (note that this would actually require a restart instead of start, so it's probably the least desirable of the options)

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 pm2 to treat your project:

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

JSON options

Ardeth answered 7/6, 2015 at 16:17 Comment(4)
What is the id_project in here pm2 start id_project --watch?Aboveground
@testteam I've update the answer to update the links and provide some more clarityArdeth
This does not reload the app, it restarts itAudiology
does watch increases more memory resource?Shakespeare
L
7

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

# 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
Lowbrow answered 10/7, 2018 at 11:48 Comment(0)
S
5

By default, pm2 doesn’t automatically refresh our server every time we change files. You need to start your pm2 project with the --watch cli argument in order to tell pm2 to refresh when files have changed:

pm2 start id_project --watch

check out the docs for more details, or @rogier-spieker answer which is more detailed.

Swaney answered 21/4, 2015 at 8:33 Comment(4)
ok, so... my server.js uses `#!/usr/local/bin/nodemon" yet pm2 doesn't seem to invoke nodemonSpectacled
sorry, @Michael. like I said, I don't actually use pm2 so I can't help any further than I already did. I just said what I said since it sounds like it will solve the OP's problem. Try looking at other answers in this thread. maybe they can help you out.Swaney
Personally, it looks like the other answer in this thread should get the accepted answer. I can't even delete this answer as long as it is the accepted answer.Swaney
@Swaney I don't want to downvote, I think Mr.Dung should change the accepted answer... although I don't expect he will, as almost 6 years have passedMcelhaney
D
4

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 start index.js --watch

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

pm2 stop 0
pm2 stop index

other two options are below

npx supervisor index.js
nodemon index.js
Dena answered 4/9, 2019 at 14:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.