Can pm2 run an 'npm start' script

前端 未结 17 1173
心在旅途
心在旅途 2020-12-04 04:45

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

相关标签:
17条回答
  • 2020-12-04 05:20

    pm2 start npm --name "custom_pm2_name" -- run prod

    "scripts": {
        "prod": "nodemon --exec babel-node ./src/index.js"
      }
    

    This worked for me when the others didnt

    0 讨论(0)
  • 2020-12-04 05:23

    To use npm run

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

    0 讨论(0)
  • 2020-12-04 05:25

    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.

    0 讨论(0)
  • 2020-12-04 05:27

    I needed to run a specific npm script on my app in pm2 (for each env) In my case, it was when I created a staging/test service

    The command that worked for me (the args must be forwarded that way):

    pm2 start npm --name "my-app-name" -- run "npm:script"
    

    examples:

    pm2 start npm --name "myApp" -- run "start:test"
    
    pm2 start npm --name "myApp" -- run "start:staging"
    
    pm2 start npm --name "myApp" -- run "start:production"
    

    Hope it helped

    0 讨论(0)
  • 2020-12-04 05:30

    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
    
    0 讨论(0)
提交回复
热议问题