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

4

9

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?

Roband answered 3/7, 2020 at 7:21 Comment(4)
did you read the pm2 cli commands? your missing parameters.Cochise
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?Roband
@Cochise 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!Roband
yes, you need to install it globally.Cochise
N
6

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

Nonobjective answered 15/6, 2021 at 6:54 Comment(1)
And how do I stop it?Stocktonontees
D
5

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

Doble answered 7/10, 2020 at 11:43 Comment(0)
S
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

Siler answered 19/9, 2022 at 6:50 Comment(0)
G
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

Gymnasiarch answered 17/1, 2023 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.