PM2 not taking the latest version of nodejs installed
Asked Answered
E

2

7

Previously, I had node version v0.10.46 installed on my ec2 server. For a recent project, I decided to give pm2 a try and installed pm2 using npm install pm2 -g.

But, pm2 start index.js errored out since the project was using some ES6 syntax with arrow functions and let keyword.

enter image description here

Therefore, I updated the node version using nvm to latest v6.9.1, which is also the same version which we used for developing on local (windows).

However, pm2 start index.js again errored out with the same error:enter image description here

pm2 show index hinted that the nodejs version was still not updated. I removed the old nodejs version, installed pm2 again, still of no avail.

I tried with other methods as well, by using:

pm2 start index.js --interpreter=~/.nvm/versions/node/v6.9.1/bin/node

to force pm2 to use the latest installed version of node. Every single try gave me the same errors with the same version of nodejs. Why is Pm2 not taking the latest version of node and sticking with 0.10.46?

If it helps:

which node
~/.nvm/versions/node/v6.9.1/bin/node
which pm2
~/.nvm/versions/node/v6.9.1/bin/pm2

Also, v0.10.46 was NOT installed using nvm.

Edit: Here are the running pm2 daemons, using ps -ef | grep pm2:

pm2 daemons

Note that ec2-user is the logged in user and I also tried the same with root user. I installed nvm running node v6.9.1 and pm2 as root user as well with no success. I get the same error.

Eatmon answered 17/1, 2017 at 18:23 Comment(2)
is the pm2 daemon running as the same user as you are logged in as, or is it a different user?Spectral
@Spectral updated the question, hope that helps. It is the same user.Eatmon
A
7

NVM allows you to run multiple version of node at a single time (between multiple shells). This means that when you run nvm use you are using that version of node within the context of that running shell.

Given PM2 runs as a daemon, I believe it kicks off its own process which is why it is not using the current nvm selected version.

This GitHub issue shows the usage of the interpreter flag which might be helpful for your specific issue https://github.com/Unitech/pm2/issues/1034


If the actual issue here is with the PM2 process needing to be running a specific NodeJS version, instead of an application PM2 is spawning, restarting the PM2 dameon itself after running the nvm use would have it startup with the current version of Node selected by nvm.

Adams answered 17/1, 2017 at 19:57 Comment(8)
Thank you for your reply. I had already checked the above thread and tried with --interpreter command. But it didnt seem to work.Eatmon
@Eatmon Just to elaborate, are you trying to get the Node application to run under a specific version of Node or the actual PM2 daemon?Adams
the actual pm2 daemon. Based on the hint I killed the daemon process and restarted. Voila, it worked. pm2 daemon updated with latest version of nodeEatmon
@Eatmon Perfect! Yeah being it is an actual separate process that would have been the next suggestion! Glad it got working!Adams
thanks can you update your answer so that I can accept it.Eatmon
very helpful question and answer. Save me a lot of timeCrusade
I had this problem only with cluster mode... killing pm2 process was THE solution. Tks !Castlereagh
I tried all the above but it's still showing the old node version..Olshausen
S
0

If you want pm2 to use a specific version of node, use the following two directives in your ecosystem.config.js file:

{
    ...
    exec_mode: "fork",
    interpreter: "[email protected]", // or any installed version
    ...
}

It is mandatory to use exec_mode: "fork", otherwise your node application will be create as a child process of your main pm2 process, which uses the node version the system is currently using.

Spermatogonium answered 7/5, 2023 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.