Proper way to update PM2 after updating Node.js - node.js

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?

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

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

In console :
pm2 save --First make sure that you saved correctly all your processes
npm install pm2 -g --Then install the latest PM2 version from NPM
pm2 update --And finally update the in-memory PM2 process

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.

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.

Related

PM2 Process Manager is not showing the actual version from each project package.json version but NVM version number

I'm running PM2 list on MacOS, and those projects node are running of the nvm managed node version. by right the expectation on the versioning column it should be showing individual node process version based on their package.json version number respectively.
but it's showing the version of nvm (node version manager). which is odd. Anyone has a clue or is this a bug from PM2 ? It happens for the couple of teammate who are working on MacOS either.
I finally understand the logic how pm2 works.
the current setup node was managed by nvm and pm2 is installed globally inside the node version.
when those process above was started by using the following style:
{
...
script: 'npm',
args: 'run start:auth'
...
}
in the pm2 config.js (start script), it indirectly ask PM2 to refer to the nvm folder, to use the npm there. at such when we inspect the process by invoking pm2 info auth it shows this line, this gave us the hint, pm2 would read the package.json resides in the ~/.nvm folder, thus it becomes the version from nvm.
Therefore the mystery is resolved.
NOTE: PM2 would read the first available package.json from the repository root, or somewhere available within the script path (e.g. when we do the pm2 info <process>). with that said, in normal circumstance we only use pm2 in a production environment, and we tend to use it to start the actual *.js script, and it would process the package.json correctly without any issues.

IIS npm start from webconfig

I'm deploying my node express project with iis. pm2 don't restart after then server rebooting. can I npm start or pm2 start on webconfig file? what should I do?
when you deploy your project use at the end
pm2 save
pm2 startup
based on documentation
pm2 Save
Once you started all the applications you want to manage, you have to save the list you wanna respawn at machine reboot
Pm2 Startup
PM2 can generate startup scripts and configure them in order to keep your process list intact across expected or unexpected machine restarts
to run the node js application after the user logged out you could try to use Forever and or install forever-win using npm -g install forever-win and do then forever start app.js.

How to use a local version of pm2 in node_modules directory to keep a server alive?

I want to keep my node server alive. Therefore I use pm2 but if I try to start my server with
pm2 start index.js
I get the message:
pm2: command not found
So, I wanted to ask how to use local pm2 in node_modules directory without installing pm2 globally.
Do I have to register pm2 in my index.js?
Can anyone provide some information about the command to start a server via pm2 which is locally installed?
Actually you can install it as a local project dependency and then run it directly with the path inside node_modules: node ./node_modules/pm2/bin/pm2 start
That way you can use it in a npm script for example.
I use this for a project that needs to run offline, bundling everything in the CI and running it locally then.
However as of lately I get some problems with the deamon starting under Windows that way. Not yet sure if it is a Windows problem or a problem with starting pm2 this way (as the globally installed version still works properly).
Try,npx pm2 start index.js. Read this article to learn about npx
If you are on AWS EC2 instance you can run the command from this path:
C:\Users\Administrator\AppData\Roaming\npm\pm2 start C:\project\app.js
In my case pm2 was installed but the err was pm2 npt found
so i ran pm2 command from that path and i worked
If you use npm, simply write in your package.json / "scripts":
"pm2": "npx pm2 start index.js -i max"
then run the script with npm run pm2
it will start your index.js with max available cluster workers

Nodejs automatically shuts after running for some time

My environment is ubuntu 16.04 with npm 5.0.0 and node 8.0.0 installed. I created an express application and started it with nohup npm start & in the application root directory. However, a couple of minutes later the app became unreachable and the process was lost. I tried restarting several time but the process always automatically exits. How can I start a long-running nodejs app?
Use pm2 daemon for setting Up a Node.js Application for Production on Ubuntu 16.04
Follow commands
sudo npm install -g pm2
pm2 start server.js //Yor main node server.js
pm2 stop server //no need of giving .js , .js only required at start of process
pm2 restart server //for any changes are done to restart
For more You can follow digital ocean tutorials for more details from below link
Digital_Ocean_Link

How to start the node.js application using pm2

I have installed pm2 module using the following command:
npm install pm2#latest
Then I tried to start my test application using pm2 as follows:
$ pm2 start test.js
It throws the following error:
'pm2' is not recognized as an internal or external command
Do i need to set environment variable for pm2?
You need to install PM2 globally via npm install --global pm2#latest and if you want to use the local version, try ./node_modules/.bin/pm2 start test.js.
After installing PM2, we may need to add following value to path variable under Environment Variables
C:\Users\USERNAME\AppData\Roaming\npm
After adding, reopen the command prompt.
You might installed the pm2 locally instead of global scope, this is due to missing -g parameter in your installation command.
npm install -g pm2
or
yan add -g pm2
If you tried npm install pm2 then the module will install locally to the app that you are currently developing from there you can invoke the pm2 using,
./node_modules/pm2/bin/pm2 start index.js
But it won't mostly work on windows. Try to use the global install option.
You are getting the same error after global install option then add the npm path in your environment variables.

Resources