how to run "npm start" by PM2?
Asked Answered
T

5

12

I have seen many questions here but that was not working for me. That why asking this question?

I am using DigitalOcean Ubuntu 16.04 server. I have a node project run by "npm start".

Script is:

"scripts": {    
    "start": "node ./bin/www"
}

generally pm2 work for

pm2 start app.js

As my script is like this and run by npm start how can I run my server forever.

Thereabouts answered 30/9, 2018 at 6:21 Comment(4)
Possible duplicate of Can pm2 run an 'npm start' scriptLian
I have seen the question but not working I have mention aboveThereabouts
Then it's specific to your case, and it's unclear why it doesn't work for you. The question contains correct answers that are expected to work in general. Consider updating the question with your current attempt and all information that could help to resolve the problem (logs, error messages, etc).Lian
The question contain correct answer right but have you read the comments below of the correct answer.Thereabouts
R
9

You can run built-in npm scripts like this:

pm2 start npm -- start

If you have a custom script, you can run like this:

pm2 start npm -- run custom

--

In your case, pm2 start npm -- start would run node ./bin/www. Change the start script to node app.js if you want to run node app.js.

Renunciation answered 30/9, 2018 at 6:24 Comment(8)
This is not working. showing Online but server not running.Thereabouts
Did you check logs? You can display logs using pm2 logs <app_name>Renunciation
There have no error and in the console its showing Online but when I am browsing its showing "can’t reach this page"..Thereabouts
The thing is pm2 start app.js is not equivalent of pm2 start npm -- start since npm start == node ./bin/www in your case.Renunciation
Yes exactly, if you look at the question. It's not node app.js so they would not equivalent. Why downvote though?Renunciation
I don,t have the ability to give down vote. don,t know bro sorry !!Thereabouts
Probably, the guy who has just deleted his comment. Never mind, I have updated the answer. I hope it would help.Renunciation
Thanks. I am trying If workes for me I will click right button must !!Thereabouts
B
5

Yes, you can do it very efficiently by using a pm2 config (json) file with elegance.

package.json file (containing below example scripts)

"scripts": {
    "start": "concurrently npm:server npm:dev",
    "dev": "react-scripts start",
    "build": "node ./scripts/build.js",
    "eject": "react-scripts eject",
    "lint": "eslint src server",
    "shivkumarscript": "ts-node -T -P server/tsconfig.json server/index.ts"
  }

Suppose we want to run the script named as 'shivkumarscript' with pm2 utility. So, our pm2 config file should be like below, containing 'script' key with value as 'npm' and 'args' key with value as 'run '. Script name is 'shivkumarscript' in our case.

ecosystem.config.json file

module.exports = {
    apps: [
        {
            name: "NodeServer",
            script: "npm",
            automation: false,
            args: "run shivkumarscript",
            env: {
                NODE_ENV: "development"
            },
            env_production: {
                NODE_ENV: "production"
            }
        }
    ]
}

Assuming that you have already installed Node.js, NPM and PM2 on your machine. Then below should be the command to start the application through pm2 which will in turn run the npm script (command line mentioned in your application's package.json file):

For production environment:

pm2 start ecosystem.config.js --env production --only NodeServer

For development environment:

pm2 start ecosystem.config.js --only NodeServer

...And Boooom! guys

Bonzer answered 11/8, 2021 at 20:32 Comment(0)
R
1

You can do something like this you can replace Test Bot with the name you want and npm start with the command you want

pm2 start "npm start" --name "Test Bot"
Ringster answered 22/8, 2023 at 8:55 Comment(0)
U
0

My application is called 'app.js'. I have the exact same start script for an express Node.js application.

And after googling tons this solved my problem, instead of calling pm2 start app.js.

pm2 start ./bin/www
Unleavened answered 1/12, 2021 at 15:46 Comment(0)
P
0

for this first, you need to create a file run.js and paste the below code on that.

const { spawn } = require('child_process');
//here npm.cmd for windows.for others only use npm
const workerProcess = spawn('npm.cmd', ['start']); 

  workerProcess.stdout.on('data', function (data) {  
      console.log('stdout: ' + data);  
   });  
 workerProcess.stderr.on('data', function (data) {  
      console.log('stderr: ' + data);  
   });  
 workerProcess.on('close', function (code) {  
      console.log('child process exited with code ' + code);  
   });  

and run this file with pm2.

pm2 start run.js 
Ploce answered 21/10, 2022 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.