Pm2: Use npm "start" script when running PM2 "start" command (if available)

Created on 29 May 2015  路  14Comments  路  Source: Unitech/pm2

NPM allows a start script to be defined inside of the package.json like so...

{
    "name": "my_app",
    "scripts": [
        "start": "node main.js"
    ]
}

Which is then executed by running "npm start".

This issue would have the "pm2 start" command use the script defined in the "package.json" file under "start" instead of requiring it to be passed via a command line argument.

Enhancement Feature Question

Most helpful comment

Starting the command npm start with PM2 is now fully supported:

$ pm2 start npm -- start

Please refer to #2176

All 14 comments

You can use an ecosystem.json file and then call pm2 startOrReload ecosystem.json

I did notice that but it seemed like having the ability to do it from the standard npm start script would be nice as well.

It doesn't seem like it would be all that big of a change, but I would understand concerns about feature creep.

Still, if I implemented this along with necessary tests and documentation is it possible the changes would be merged?

Thanks.

I think the solution proposed here is:

{
  "name": "my_app",
  "scripts": {
    "start": "pm2 startOrReload ecosystem.json"
  }
}

It will allow you to use pm2 by just running npm start instead of pm2 start, that's transparent for the user.

I wish I could start pm2 from ecosystem.json _Npm modules_ in a way like @eTorAken said:

{
  "name": "my_app",
  "scripts": {
     "start": "node http-server folderName -p 1111"
  }
}

instead of:

{
    "name": "my_app",
    "script": "node_modules/http-server/bin/http-server",
    "args": ["folderName ", "-p 1111"]
}

:sob:

I'm looking for the same as @andrewrothman Anyway to do this without using ecosystem? Seems highly unnecessary to have another json file to configure.

+1

+1

Please, try pm2 start "/usr/local/bin/npm" --name "myAPP" -- start .
It works for us.

@pibi It did work on my mac. But it did not work on Ubuntu...
screen shot 2015-09-07 at 8 37 09 pm
Any solution?

+1

@yizhiheng Try to substitute "/usr/local/bin/npm" with the full path of your installed npm. You can find it by: >which npm from the shell.

+1. The npm with package.json is the main configuration file. Pm2 should run scripts through npm thus you have access to npm config and other goodies. This works for me (run is optional):

pm2 start npm --name "{app_name}" -- run {script_name}

I know it's a little bit off topic but I wanna share my workaround for running scripts through npm in production. It took me quite some time for this to work and this discussion helped. I first create a new user, I switch to the new user (sudo su - {new user}), then I setup the nvm with the latest node version and the latest pm2 package (v0.15.6 at the moment), I run pm2 startup command and I run the output string using the sudo user. Because pm2 is not using the interactive ssh mode, I open /home/{new user}/.bashrc and comment out lines that start with a comment If not running interactively, don't do anything. In my app (on my local machine) I define only the deploy key inside my package.json file (no apps key).

  "deploy": {
    "production": {
      "user": "worker",
      "host": "X.X.X.X",
      "ref": "origin/master",
      "repo": "git@mydomain:user/repo.git",
      "path": "/home/worker/app",
      "post-deploy": "npm install && pm2 restart {app name}"
    }
  }

I make sure that I can ssh to the server using the user mentioned above, then I execute deploy commands.

pm2 deploy production setup
pm2 deploy production update
pm2 deploy production exec "npm config set {app name}:{env variable} {env variable value}" // beauty!
pm2 deploy production exec 'pm2 start npm --name "{app name}" -- start'
pm2 deploy production exec 'pm2 save'

Now I can deploy my app with the pm2 deploy production command and the app will also be restarted.

:+1: @xpepermint that was very helpful -- I was finding that I was _not_ using npm when starting apps with pm2 and it felt unclean. Now I am cleansed :stuck_out_tongue_winking_eye:

Starting the command npm start with PM2 is now fully supported:

$ pm2 start npm -- start

Please refer to #2176

Was this page helpful?
0 / 5 - 0 ratings

Related issues

phra picture phra  路  3Comments

morfies picture morfies  路  3Comments

lefam picture lefam  路  3Comments

rajendar38 picture rajendar38  路  3Comments

webchaz picture webchaz  路  3Comments