32

There are several different versions of node running on our linux server, And my service is based on node v0.11.14. However, other people's code have to run on lower version of node(lower than v0.11) otherwise their services will be out of service. So I can't define the global node version as v0.11. I just want to run pm2 to monitor my service based on node v0.11.

Is there anyway to run my pm2 on node v0.11 without changing the global node version? Thanks

hwoarangzk
  • 337
  • 1
  • 3
  • 8

5 Answers5

48

Use pm2 and specify node version using --interpreter flag with node version absolute path:

sudo pm2 start app.js --interpreter=/home/ken/.nvm/v4.4.2/bin/node

or

sudo pm2 start app.js --interpreter=/home/ken/.nvm/v7.4.0/bin/node

etc..

If you change the node version wherever I mentioned --interpreter="***.." the app will run in exact node version.

Once you completed the above approach to verify use following command

sudo pm2 show 'app name'
KEN
  • 503
  • 4
  • 7
  • On ubuntu for `nvm version 0.33.5` I had to use this: `sudo pm2 start app.js --interpreter=/home/ken/.nvm/verions/node/v7.4.0/bin/node` – sujeet Feb 09 '20 at 04:23
  • 1
    it does not work in cluster mode. e.g. pm2 start app.js -i 2 --interpreter="***.." <<=== not working. – Waqas Malik Sep 12 '22 at 13:08
14

Please read the following thread: Using different versions of node via nvm for each app

I believe you wanted to hack around the nvm, but believe me it can save much of your time.

You can find a comment in the thread from the pm2 owner itself, which states you can run multiple apps on different node versions, here is a JSON conf content:

{ 
  apps : [{
    name : 'API',
    script : 'api.js',
    interpreter : 'node@6.9.1'
 }]
}

If you are interested in the solution above, please read through the documentation here: PM2 - process file

PM2 empowers your process management workflow. It allows you to fine-tune the behavior, options, environment variables, logs files of each application via a process file. It’s particularly useful for micro-service based applications.

Configuration format supported are Javascript, JSON and YAML.

Dávid Horváth
  • 311
  • 2
  • 6
  • 7
    Something to keep in mind when doing this if using cluster mode.... [PM2][WARN] Choosing the Node.js version in cluster mode is not supported. Which means, all your apps will need to be using the same version of node that pm2 is executed from. – Marc Aug 30 '17 at 16:49
  • Where do you put this file? In the app's directory? just pm2.json in the file's dir? – the_gesslar Aug 19 '23 at 20:39
11

To run several version at the same time. In pm2, you can use the --interpreter options and specify the path to the node version you want.

If you use n for version run n bin v4.2.0 to get the path to this node version.

Sylvain
  • 542
  • 6
  • 20
0

If this is really not working for you, here are some debug steps:

make sure to use interpreter and a full absolute path to the interpreter on the ecosystem file:

interpreter: "/home/Mattia/.nvm/versions/node/v18.17.1/bin/node"

Update pm2 to latest version:

$ npm install pm2@latest -g

then kill pm2 processes:

pm2 kill

and restart the pm2 process.

Mattia Rasulo
  • 1,236
  • 10
  • 15
-3

install https://github.com/creationix/nvm

then install specific node version:

nvm install 0.11.14

than in a shell use the specific version:

nvm use 0.11.14

node -v // v0.11.14
Yevgen Safronov
  • 3,977
  • 1
  • 27
  • 38
  • Thanks for your answer, but will `nvm use 0.11.14` let the global node version get changed to v0.11.14? – hwoarangzk Apr 21 '15 at 08:50
  • 1
    Nvm basically a shell function, so no global changes at all, the version set with nvm is bound to shell session – Yevgen Safronov Apr 21 '15 at 08:54
  • FYI, you can install one nvm per user and one pm2 per user globally on different node versions with nvm. I'd add that node `11` is unstable but `12` is not ;). – soyuka Apr 22 '15 at 07:36