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.

A1Gard
  • 4,070
  • 4
  • 31
  • 55
  • Possible duplicate of [Can pm2 run an 'npm start' script](https://stackoverflow.com/questions/31579509/can-pm2-run-an-npm-start-script) – Estus Flask Sep 30 '18 at 07:03
  • I have seen the question but not working I have mention above –  Sep 30 '18 at 07:19
  • 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). – Estus Flask Sep 30 '18 at 07:22
  • The question contain correct answer right but have you read the comments below of the correct answer. –  Sep 30 '18 at 07:47

5 Answers5

8

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.

hisener
  • 1,431
  • 1
  • 17
  • 23
  • 3
    This is not working. showing Online but server not running. –  Sep 30 '18 at 06:26
  • Did you check logs? You can display logs using `pm2 logs ` – hisener Sep 30 '18 at 06:27
  • There have no error and in the console its showing Online but when I am browsing its showing "can’t reach this page".. –  Sep 30 '18 at 06:30
  • The thing is `pm2 start app.js` is not equivalent of `pm2 start npm -- start` since `npm start` == `node ./bin/www` in your case. – hisener Sep 30 '18 at 06:31
  • Yes exactly, if you look at the question. It's not `node app.js` so they would not equivalent. Why downvote though? – hisener Sep 30 '18 at 06:34
  • I don,t have the ability to give down vote. don,t know bro sorry !! –  Sep 30 '18 at 06:35
  • Probably, the guy who has just deleted his comment. Never mind, I have updated the answer. I hope it would help. – hisener Sep 30 '18 at 06:37
  • Thanks. I am trying If workes for me I will click right button must !! –  Sep 30 '18 at 06:49
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

Shiv
  • 3,069
  • 1
  • 24
  • 17
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
Bill Lau
  • 15
  • 5
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 
0

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"
Miki
  • 157
  • 1
  • 8