7

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?

Operator
  • 85
  • 1
  • 7
  • did you read the pm2 cli commands? your missing parameters. – Talg123 Jul 03 '20 at 07:23
  • Yes, I read it but not entirely. There stood to use pm2 start any application. I will read it again but nevertheless could you provide an answer if you know the answer? – Operator Jul 03 '20 at 07:32
  • 1
    @Talg123 I read a bit and saw that they install it globally. Do I have to install it globally? Can't I use it locally somehow? I would mark your answer if you can help! – Operator Jul 03 '20 at 07:40
  • yes, you need to install it globally. – Talg123 Jul 03 '20 at 08:02

4 Answers4

5

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).

Robert Pazurek
  • 106
  • 1
  • 4
3

Try,npx pm2 start index.js. Read this article to learn about npx

0

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

jazeb007
  • 578
  • 1
  • 5
  • 11
0

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

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Mute
  • 1
  • 1