How to Update the Node.js Version in a PM2 Instance
Updating the Node.js version in a PM2-managed application requires a few key steps to ensure a smooth transition without losing your process state. Follow these steps to update Node.js while keeping your PM2 processes intact.
Step 1: Save the Current PM2 State
Before updating Node.js, save the current PM2 process list to restore it later. Run:
pm2 save
This ensures that your running applications and their configurations are backed up.
Step 2: Disable PM2 Auto-Start
If PM2 is set up to start automatically on system boot, disable it before updating Node.js:
pm2 unstartup
This prevents conflicts during the update process.
Step 3: Update Node.js
Use a version manager like NVM (Node Version Manager) to update Node.js.
Refer my Blogs to Update / Install
Verify the installation:
node -v
Step 4: Reinstall PM2
Since PM2 is installed globally, updating Node.js may remove it. Reinstall PM2 using:
npm install -g pm2
Step 5: Update PM2
Ensure PM2 is updated to the latest version to avoid compatibility issues:
pm2 update
Step 6: Restore the Previous PM2 State
Reload the saved process list to restore your applications:
pm2 resurrect
Step 7: Re-enable PM2 Auto-Start
If you want PM2 to start automatically on system reboot, reconfigure it with:
pm2 startup
Follow the on-screen instructions to complete the setup.
Step 8: Reload Applications with the New Node.js Version
Finally, reload your applications with the updated environment variables:
pm2 reload <app-name or index> --update-env
Conclusion
By following these steps, you can seamlessly update your Node.js version while ensuring that your PM2-managed applications continue running smoothly.
Let me know if you need further clarification! 🚀
Originally published at https://blog.udhay.dev on Feb 21, 2025.