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

Viewed 3766

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?

3 Answers

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

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

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

Related