Proper way to update PM2 after updating Node.js
Asked Answered
K

8

12

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?

Katonah answered 5/10, 2014 at 17:7 Comment(0)
U
7

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.

Undulate answered 5/10, 2014 at 17:16 Comment(1)
So am I correct in the thought that PM2 running as daemon was controlling programs in updated node v10.32, but the pm2 <command> features were unreachable until the pm2 package was re-installed? Or was PM2 still somehow attached and running the programs under it's control in Node v10.16 until the package was updated? Thanks for the quick response. NelsKatonah
M
11

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
Myxomatosis answered 29/8, 2020 at 7:56 Comment(0)
M
9

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
Max answered 6/9, 2017 at 9:43 Comment(0)
F
8

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
Frunze answered 4/12, 2018 at 14:31 Comment(1)
pm2 delete 0 works! Besides, use pm2 show 0 will show the node version that a app use.Chester
U
7

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.

Undulate answered 5/10, 2014 at 17:16 Comment(1)
So am I correct in the thought that PM2 running as daemon was controlling programs in updated node v10.32, but the pm2 <command> features were unreachable until the pm2 package was re-installed? Or was PM2 still somehow attached and running the programs under it's control in Node v10.16 until the package was updated? Thanks for the quick response. NelsKatonah
S
2

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.

Symbolize answered 4/9, 2021 at 14:23 Comment(0)
T
0

I tried a few of the options I researched. In the end I uninstalled PM2, after updating Node (via NVM), and reinstalled it. That worked.

Trent answered 28/12, 2023 at 14:47 Comment(0)
S
0

Below is what works for me after upgrading Node from 16.0.0 to 16.0.2

  1. Switch back to previous Node version

    nvm use <previous node version>

  2. Save pm2 process

    pm2 save

  3. Switch to new Node version

    nvm use <new node version>

  4. Install pm2 again. This will install and start the pm2 process using saved processes in step 2

    npm i -g pm2 && pm2 update

Selby answered 20/1, 2024 at 5:19 Comment(0)
T
-1

Check the NodeJS version (LTS).

Command to check the latest Package of pm2:

npm install pm2@latest -g

Command to update the pm2:

pm2 update

Command to check the pm2 version:

pm2 -version
Tavie answered 30/10, 2023 at 8:40 Comment(2)
npm install pm2@latest -g installs pm2, it does not just check, it may be semantics but please choose the most general term.Esoterica
Thanks for the response. After updating the node js version. It is mandatory to update the PM2. It works for me.Tavie

© 2022 - 2025 — McMap. All rights reserved.