How to Run a Npm Start Script With PM2
Are you looking for a way to run npm start
scripts with PM2? Or how to to do so while also using the options commands with it as well?
In this article, we will go through some examples that will help you achieve this.
If you haven't already, you can install PM2 globally on your machine with this command:
npm install pm2 -g
Once you have the NPM package installed, you'll be good to go with using the examples in this article!
Table Of Contents
NPM Start Script
To run the npm start
script with PM2, you can use the following command (make sure you call the command from inside your project folder):
pm2 start npm -- start
And you should see a similar output in your command prompt:
[PM2] Starting /usr/bin/npm in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ npm │ 0 │ fork │ 3524 │ online │ 0 │ 0s │ 4.0 KB │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
NPM Start Script With the App Name Option
Here is an example of running an npm start
script with the --name
Pm2 option to name your application.
Here is the command (replace APP_NAME
with the name of your app):
pm2 start --name=APP_NAME npm -- start
And you should see a similar output in your command prompt:
[PM2] Starting /usr/bin/npm in fork_mode (1 instance)
[PM2] Done.
┌──────────┬────┬──────┬──────┬────────┬─────────┬────────┬─────────────┬──────────┐
│ App name │ id │ mode │ pid │ status │ restart │ uptime │ memory │ watching │
├──────────┼────┼──────┼──────┼────────┼─────────┼────────┼─────────────┼──────────┤
│ APP_NAME │ 0 │ fork │ 3524 │ online │ 0 │ 0s │ 4.0 KB │ disabled │
└──────────┴────┴──────┴──────┴────────┴─────────┴────────┴─────────────┴──────────┘
Use `pm2 show <id|name>` to get more details about an app
NPM Start Script With Multiple Options
In the last example, we will cover how to run an NPM script with multiple options.
Here is the command (replace APP_NAME
with your app name):
pm2 start --name=APP_NAME --no-autorestart npm -- start
You should see the same output as the previous examples.