My node-app is running in PM2. When I pull the latest version of my app off github and rebuild it, the site during the building process defaults to a much older version (probably the first version when I initially launched the daemon). How can I make it show the latest version before the fetch, while I rebuild to the really latest version?
-
1having exactly the same problem! Any luck with it? – michelepatrassi Jan 22 '19 at 19:25
-
Yeah actually hadn't solved it back when I posted this. But, I don't need it for the time being. So hope you can find the answer. – Ansjovis86 Jan 23 '19 at 12:57
-
1found! The process itself was ok, was exactly the latest version. The problem I had was that the system node version was 8.something, while the latest version of my process needed 10.10.0. My CLI used the 10, while the server the 8. Setting the system node as 10 solved my issue. – michelepatrassi Jan 23 '19 at 19:46
4 Answers
To serve the latest code in the directory, run:
$ pm2 reload APP_NAME
You can find out APP_NAME by
$ pm2 list
When you fetch the latest code and want to run it, restart the server with
$ pm2 reload APP_NAME
To Start a server for the first time with a specific name
$ pm2 start path/to/index.js --name "api"
Note: On linux you might have to run pm2 with sudo

- 998
- 3
- 13
- 31
-
1this is not working, when I reload it and rebuild it, it defaults to a version of about a month ago – Ansjovis86 May 24 '18 at 23:52
-
1
-
1This isn't working for me either. Error logs still displaying file names long since deleted from project. – Milambardo Dec 12 '19 at 12:03
I had the same problem, i restarted the app and had to clear the cloudfront cache by creating invalidation.

- 13
- 1
- 5
-
4While this code may solve the question, [including an explanation](https://meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion – Muhammad Dyas Yaskur Feb 03 '20 at 07:14
I am afraid this might be a little late but I found a fix for this if any of the above answers did not work for you. For context, I have a MERN web app with a client side folder (react-app).
Structure of my file is similar to this:
app
|
|-- client (react-app)
|-- build
|-- node_modules
|-- public
|-- src
|-- package-lock.json
|-- package.json
|-- controllers
|-- models
|-- routers
|-- node_modules
|-- .env
|-- package-lock.json
|-- package.json
|-- server.js
As you can see when I run pm2 I will simply run
pm2 start server.js
I had the same issue, my project shows the same front-end version that was much earlier than the one I am running.
Some steps I did to debug and detect the cause of my issue:
- Make sure pm2 is running your program on the most recent node version.
- Check if the server (back-end) is running on the newest updated version of your repo.
- If it is, then it is an issue isolated with your front-end. Try running
npm run build
in your client folder to generate the most updated front-end build. - If it is not, there may be an issue in how your git repo is recognized (unlikely, but worth a shot to try move your project to a fresh new repo).
My issue was that I didn't npm run build
the newest version of my front-end, hence it was still showing a version ages ago.
Note: npm run build
is a valid script for me because I downloaded the package react-scripts
in my client side. Just chuck in "build": "react-scripts build",
in your scripts in the client side package.json file.
Note 2: I am deploying this on a Droplet in Digital Ocean, and I have found a thread that may be relevant to your issue: How do I force PM2 to use the latest version of my app?

- 23
- 4
I had the same issue. To my mind the topic is that when you run those commands within the PM2 daemon PM2 restart all
or PM2 reload APP_NAME
that the old commit is taken.
When I used the command PM2 restart all
outside the daemon then PM2 restarted with the new commit.
My approach now is that I have a script that runs in another daemon with nohup and checks if the command PM2 restart all
should be sent (,but then it comes from outside the PM2 daemon).
Maybe the script below helps somebody, I spent some time with it with testing:
The script checks every 3rd second a file restart.txt
in the same folder like restart.sh
if there is the word true
in the first-line. If so, it sets the first-line to false
and calls PM restart all
. A log line is always placed to restart.log
in the same folder. The restart.sh
is executed in a nohup-daemon by pkill -9 -f restart.sh; nohup YOURPATH/restart.sh & pid=999999
, which is killing all running restart.sh-processes and starts a new one. In my application I set true
to restart.txt
where I originally called PM2 restart all
.
I find this solution a little bit of an overkill and definitely a workaround, if anybody has another, better idea, I look forward hearing it.
#restart.sh
#!/bin/bash
while true
do
FIRSTLINE=`head -n 1 ${0%/*}/restart.txt`
DATE=`date "+%Y%m%d_%H%M%S"`
if [ $FIRSTLINE = true ]
then
echo "${DATE} - restarting" > ${0%/*}/restart.log
echo false > ${0%/*}/restart.txt
pm2 restart all
else
echo "${DATE} - Not restarting" > ${0%/*}/restart.log
fi
sleep 3
done

- 323
- 4
- 15