Can pm2 run an 'npm start' script

浪尽此生 提交于 2019-11-28 02:39:54

PM2 now supports npm start:

pm2 start npm -- start

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.

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.

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

To use npm run

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

See to enable clustering:

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

What do you think?

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

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

Unfortunately no luck with running script start:prod.

"scripts": {
   "start:prod": "node index.js"
},

Command pm2 start npm -- run start:prod crashes

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.

Now, You can use after:

pm2 start npm -- start

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

pm2 start ./bin/www

can running

if you wanna multiple server deploy you can do that. instead of pm2 start npm -- start

Vidura Adikari

Don't forget the space before start

pm2 start npm --[space]start

so the correct command is:

pm2 start npm -- start
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!