Can pm2 run an 'npm start' script

node.jsShellNpmPm2

node.js Problem Overview


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" ./

node.js Solutions


Solution 1 - node.js

PM2 now supports npm start:

pm2 start npm -- start

To assign a name to the PM2 process, use the --name option:

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

Solution 2 - node.js

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.

Solution 3 - node.js

To use npm run

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

Solution 4 - node.js

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.

Solution 5 - node.js

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

Solution 6 - node.js

you need to provide app name here like myapp

pm2 start npm --name {appName} -- run {script name}

you can check it by

pm2 list

you can also add time

pm2 restart "id" --log-date-format 'DD-MM HH:mm:ss.SSS'

or

pm2 restart "id" --time

you can check logs by

pm2 log "id"

or

pm2 log "appName"

to get logs for all app

pm2 logs

Solution 7 - node.js

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

Solution 8 - node.js

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

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

Solution 9 - node.js

See to enable clustering:

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

What do you think?

Solution 10 - node.js

If you use PM2 via node modules instead of globally, you'll need to set interpreter: 'none' in order for the above solutions to work. Related docs here.

In ecosystem.config.js:

  apps: [
    {
      name: 'myApp',
      script: 'yarn',
      args: 'start',
      interpreter: 'none',
    },
  ],

Solution 11 - node.js

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

Solution 12 - node.js

For the normal user

PM2 now supports npm start:

pm2 start npm -- start

To assign a name to the PM2 process, use the "--name" option:

pm2 start npm --name "your desired app name" -- start

For the root user

sudo pm2 start npm -- start

To assign a name to the PM2 process, use the "--name" option:

sudo pm2 start npm --name "your desired app name" -- start

Solution 13 - node.js

Yes, Absolutely you can do it very efficiently by using a pm2 config (json) file with elegance.

package.json file (containing below example scripts)
"scripts": {
    "start": "concurrently npm:server npm:dev",
    "dev": "react-scripts start",
    "build": "node ./scripts/build.js",
    "eject": "react-scripts eject",
    "lint": "eslint src server",
    "shivkumarscript": "ts-node -T -P server/tsconfig.json server/index.ts"
  }

Suppose we want to run the script named as 'shivkumarscript' with pm2 utility. So, our pm2 config file should be like below, containing 'script' key with value as 'npm' and 'args' key with value as 'run

Solution 14 - node.js

You can change directory to your project

cd /my-project

then run

pm2 start "npm run start" \\ run npm script from your package.json

read more here

Solution 15 - node.js

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.

Solution 16 - node.js

pm2 start ./bin/www

can running

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

Solution 17 - node.js

Don't forget the space before start

pm2 start npm --[space]start

so the correct command is:

pm2 start npm -- start

Solution 18 - node.js

It's working fine on CentOS 7

PM2 version 4.2.1

let's take two scenarios:

1. npm start //server.js

pm2 start "npm -- start" --name myMainFile

2. npm run main //main.js

pm2 start "npm -- run main" --name myMainFile

Solution 19 - node.js

To run PM2 with npm start method and to give it a name, run this,
pm2 start npm --name "your_app_name" -- start

To run it by passing date-format for logs,
pm2 start npm --name "your_name" --log-date-format 'DD-MM HH:mm:ss.SSS' -- start

Solution 20 - node.js

Now, You can use after:

pm2 start npm -- start

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionsvnmView Question on Stackoverflow
Solution 1 - node.jsDhaval ChauhanView Answer on Stackoverflow
Solution 2 - node.jsJyotman SinghView Answer on Stackoverflow
Solution 3 - node.jscarkodView Answer on Stackoverflow
Solution 4 - node.jsjcollumView Answer on Stackoverflow
Solution 5 - node.jsTomer OmriView Answer on Stackoverflow
Solution 6 - node.jsPharaj AliView Answer on Stackoverflow
Solution 7 - node.jspeccuView Answer on Stackoverflow
Solution 8 - node.jsKARTHIKEYAN.AView Answer on Stackoverflow
Solution 9 - node.jsjdnichollscView Answer on Stackoverflow
Solution 10 - node.jsKevin CooperView Answer on Stackoverflow
Solution 11 - node.jsLuke RobertsonView Answer on Stackoverflow
Solution 12 - node.jsAshish GuptaView Answer on Stackoverflow
Solution 13 - node.jsShivamView Answer on Stackoverflow
Solution 14 - node.jsNelson FrankView Answer on Stackoverflow
Solution 15 - node.jssnozzaView Answer on Stackoverflow
Solution 16 - node.js劉謹賢View Answer on Stackoverflow
Solution 17 - node.jsVidura AdikariView Answer on Stackoverflow
Solution 18 - node.jsM. Hamza RajputView Answer on Stackoverflow
Solution 19 - node.jsAnish NairView Answer on Stackoverflow
Solution 20 - node.jsMinh DaoView Answer on Stackoverflow