Can pm2 run an 'npm start' script

2019-01-16 01:28发布

问题:

Is there a way for pm2 to run an npm start script or do you just have to run pm2 start app.js

So in development

npm start

Then in production with pm2 you would run something like

pm2 start 'npm start'

There is an equivalent way to do this in forever

forever start -c "npm start" ./

回答1:

PM2 now supports npm start:

pm2 start npm -- start


回答2:

Those who are using a configuration script like a .json file to run the pm2 process can use npm start or any other script like this -

my-app-pm2.json

{
    "apps": [
        {
            "name": "my-app",
            "script": "npm",
            "args" : "start"
        }
    ]
}

Then simply -

pm2 start my-app-pm2.json

Edit - To handle the use case when you have this configuration script in a parent directory and want to launch an app in the sub-directory then use the cwd attribute.

Assuming our app is in the sub-directory nested-app relative to this configuration file then -

{
    "apps": [
        {
            "name": "my-nested-app",
            "cwd": "./nested-app",
            "script": "npm",
            "args": "start"
        }
    ]
}

More detail here.



回答3:

I wrote shell script below (named start.sh). Because my package.json has prestart option. So I want to run npm start.

#!/bin/bash
cd /path/to/project
npm start

Then, start start.sh by pm2.

pm2 start start.sh --name appNameYouLike


回答4:

Yes. Use pm2 start npm --no-automation --name {app name} -- run {script name}. It works. The --no-automation flag is there because without it PM2 will not restart your app when it crashes.



回答5:

See to enable clustering:

pm2 start npm --name "AppName" -i 0 -- run start

What do you think?



回答6:

Unfortunately, it seems that pm2 doesn't support the exact functionality you requested https://github.com/Unitech/PM2/issues/1317.

The alternative proposed is to use a ecosystem.json file Getting started with deployment which could include setups for production and dev environments. However, this is still using npm start to bootstrap your app.



回答7:

Now, You can use after:

pm2 start npm -- start

Follow by https://github.com/Unitech/pm2/issues/1317#issuecomment-220955319



回答8:

Yes we can, now pm2 support npm start, --name to species app name.

pm2 start npm --name "app" -- start


回答9:

To use npm run

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



标签: node.js npm pm2