Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use npm "start" script when running PM2 "start" command (if available) #1317

Closed
andrewrothman opened this issue May 28, 2015 · 14 comments
Closed

Comments

@andrewrothman
Copy link

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.

@jorge-d
Copy link
Contributor

jorge-d commented May 29, 2015

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

@andrewrothman
Copy link
Author

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.

@eTorAken
Copy link

eTorAken commented Jun 3, 2015

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.

@cachaito
Copy link

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"]
}

😭

@ghost
Copy link

ghost commented Aug 2, 2015

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.

@mayeaux
Copy link

mayeaux commented Aug 17, 2015

+1

1 similar comment
@rpepato
Copy link

rpepato commented Aug 24, 2015

+1

@pibi
Copy link

pibi commented Sep 2, 2015

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

@yizhiheng
Copy link

@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?

@HackToHell
Copy link

+1

@pibi
Copy link

pibi commented Sep 9, 2015

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

@xpepermint
Copy link

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

@jcollum
Copy link

jcollum commented Jan 18, 2016

👍 @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 😜

@Unitech
Copy link
Owner

Unitech commented May 23, 2016

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

$ pm2 start npm -- start

Please refer to #2176

@Unitech Unitech closed this as completed May 23, 2016
Repository owner locked and limited conversation to collaborators Aug 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests