Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to update PM2 after updating Node.js

Tags:

node.js

pm2

After updating Node.js from v10.16 to v10.32, PM2 was not detected, however it was running fine when checked with ps aux. Even upon system reboot PM2 functioned correctly even though manual PM2 commands resulted in following type of error.

pm2 list pm2: command not found

Switching Node.js back to 10.16 and PM2 commands were again available. fyi PM2 was initially installed under v10.16.

While in v10.32 tried PM2 install command npm install pm2 -g but had to use command npm install pm2 -g --unsafe-perm to get operational.

Node.js v10.16 now runs PM2 v10.1. Node.js v10.32 now runs PM2 v10.8.

Is this the proper method to keep PM2 versions in sync and working with Node upgrades/changes? Does this need to occur after installing every new version of Node?

like image 849
Nels Avatar asked Oct 05 '14 17:10

Nels


5 Answers

It's seem there no way without re-installing PM2 after a Node update :-(

$ nvm install 6.11.3 --reinstall-packages-from=6.11.2 && nvm alias default 6.11.3
$ nvm uninstall 6.11.2
$ pm2 update # Update in memory pm2
$ pm2 startup
$ nano /etc/init.d/pm2-init.sh  # Wrong path :-(

But re-installing pm2 is not enought, some things are still broken even if it seem to work, logs are no more in real time for example My hot fix :

$ rm -rf /root/.pm2
$ pm2 reload pm2.json --env production
$ pm2 startup ubuntu
like image 178
molokoloco Avatar answered Oct 09 '22 18:10

molokoloco


In console :

  1. pm2 save --First make sure that you saved correctly all your processes
  2. npm install pm2 -g --Then install the latest PM2 version from NPM
  3. pm2 update --And finally update the in-memory PM2 process
like image 34
Semir Hodzic Avatar answered Oct 09 '22 18:10

Semir Hodzic


I tried a lot of times with differnent combinations but still seems not very stable and smart solution. Hence I am listing some of the logic I can think of something which may be you can apply and monitor the result as you upgrading and writing the scripts.

Basically in my situation we have a bunch of applications running under Node. So things getting complex when you need PM2 to launch anothe application that also installed under Node Version Managers, like NVM

Ex. I have

nvm ls
->     v14.17.6

PM2 is installed under:

which pm2
~/.nvm/versions/node/v14.17.6/bin/pm2

Since I am using App1 (a NodeJS app managed by npm). I got:

which App1
~/.nvm/versions/node/v14.17.6/bin/App1

So every time I upgrade using nvm:

nvm install --lts --reinstall-packages-from=14 --latest-npm

Then nvm using a newer version in this console. e.g. 14.7.999999

Perhapse I (at most of the time) needs to get my PM2 plus other applications upgrade at the same maintenance window, I use ncu, ncu -g and upgrade them.

Now, the applications end up with all new version. Ex. a new PM2 instance (local) and an old PM2 running (In Memory) with old comsumer applications (App1) in an old node folder. New version of App1 now exists in new Node application folder but not running.

In memory PM2 version: 5.1.0
Local PM2 version: 5.1.1

Anyway, if you don't have an upgraded version of PM2, you probably still looking for a new path of PM2 installed under your new Node folder. If not, you can install PM2 again with the upgraded node

npm i -g pm2

The thing getting worse is PM2 is in system startup scripts which needs to be re-written. Ex.

/etc/systemd/system/pm2-xx.service

So I endup with vanishing all applications:

pm2 stop app1 && pm2 delete app1
pm2 stop app2 (verdaccio json startup config) && pm2 delete app2
...
pm2 stop appN && pm2 delete appN

Then do:

pm2 update

To swap to the new PM2 instance

Then re-provision all the applications

pm2 start app1, app2, ... appN

Then do

pm2 update 

To update the list of applications, check if correct Node path is used.

If all applciation paths are corrected Do

pm2 startup systemd

And copy and run the suggested Startup Script

sudo env PATH=$PATH:/....

Finally run

pm2 save

To freeze the list on startup.

like image 35
Vincent-cm Avatar answered Sep 19 '22 17:09

Vincent-cm


Do not forget to rebuild packages after updating the version of node.js:

cd /to/root/of/your/project
npm rebuild
npm i -g pm2 && pm2 update

# here 0 and dist/main.js change for your project
pm2 delete 0 && pm2 start dist/main.js
like image 6
ktretyak Avatar answered Oct 09 '22 18:10

ktretyak


When you switch node versions, you also switch the packages, so you need to reinstall pm2 on node update. Fortunately this does not happen very often.

You could make a shell sript to do both in one go.

For the unsafe-perm thing, it comes only if you install pm2 as root. It makes sense when you think that pm2 has quite a lot of control over your machine's processes.

like image 3
xShirase Avatar answered Oct 09 '22 18:10

xShirase