A Step by Step Guide To NPM Run Start At The Background

A Step by Step Guide To NPM Run Start At The Background
3709 Views

Introduction

In this tutorial, we will walk you through the process of setting up and executing NPM scripts in the background, allowing you to run tasks continuously without tying up your terminal or command prompt window.

1 banner

So I have a (Vue/Angular/React.JS) web project that needs to run in the background.

Why Stop Project in backgroud?

When I quit or interrupt the SSH connection by pressing Ctrl-C, the ongoing session would terminate, resulting in undesired consequences.

2 banner

How can this be avoided?

To avoid terminating the ongoing session when quitting or interrupting the SSH connection, you can use this code:

(npm run start&)

When you run this command then you got the below result 👇

3 banner

Drawback of This code

The drawback of using “npm run start&” is that you do not have direct control over the running process. I need to manually terminate the process like follows:

4 banner

Note:  It is important to remember that the output generated by web applications is stored in a log file located in the default log directory of npm.

PM2 process manager

PM2 is a popular process manager for Node.js applications that provides advanced features for managing and deploying applications in a production environment. With PM2, you can easily start, stop, restart, monitor, and manage multiple Node.js processes, ensuring high availability and seamless operation.

Install PM2

PM2 can be installed by running the command “npm install -g pm2” after ensuring Node.js and NPM are installed on your system.

sudo npm install pm2 -g

Run PM2

pm2 --name HelloWorld start npm -- start
5 banner

Please keep in mind the following points:

  • Ensure that the npm script in packages.json is started (for VUE.JS, the default script is serve).
  • While not required, providing a name for the running processes is tremendously helpful in identifying them.

Listing PM2 Running Processes

Run the command “pm2 pst” to display a list of all running processes. The list will include information such as process ID, name, status, and resource usage.

Use this command to quickly view and monitor the status of your managed processes.

pm2 ps
6 banner

Stopping PM2 Running Processes

Run the command “pm2 delete 0” to stop a specific process by its name. Alternatively, use “pm2 delete 0” to stop all running processes managed by PM2.

pm2 delete 0
7 banner

Checking logs of PM2

Run the command “pm2 logs ” to view the logs of a specific process by its name. Use “pm2 logs” to view logs of all running processes managed by PM2.

pm2 logs
8 banner

Last Words

Running NPM start in the background is a powerful technique that allows you to keep your Node.js application running persistently, even after closing your terminal session.

By Following upper step-by-step guide, you have learned how to effortlessly run your application in the background, freeing up your terminal for other tasks. This method available on CodexCoach is particularly useful for long-running processes and applications that need to stay available continuously. 

FAQs

Can I run NPM start in the background on any operating system?

Yes, you can run NPM start in the background on any operating system that supports Node.js and NPM. This guide provides general instructions that are applicable to macOS, Linux, and Windows.

Can I run other NPM scripts in the background as well?

Absolutely! The process described in this guide is not limited to the ‘start’ script alone. You can use the same steps to run any other NPM script in the background by replacing ‘start’ in the command with the desired script name.

Was this article helpful?
YesNo

Leave a comment

Your email address will not be published. Required fields are marked *