Open In App

How to NPM Run Start At The Background?

Last Updated : 17 Oct, 2024
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Running npm start or npm run start in the background is one of those things that may not realized until you are knee-deep in a project. Whether you are building a web application, working with microservice, or just trying to keep your terminal free from having a server run without locking your terminal is a massive time-saver. So, let's explore how we can get this done - in such an efficient manner.

These are the following topics and approaches that we are going to discuss:

What Does It Mean by run npm in the Background?

When we run npm start, usually our terminal gets locked up, dedicated to keeping that process running. You cannot type any more commands in the same terminal window without stopping the server first. While this works fine for quick tests it is not ideal when we want to keep working while our server keeps running in the background.

Running npm run start in the background simply means that we can free up our terminal while the server continues running. It's like asking your terminal, "Hey you will keep doing that task quietly while I move on with other things?"

Why would you want to do This?

Here are a few scenarios where running npm starts in the background :

  • When we are running multiple services or servers and do not want 4-5 different terminals open for each.
  • When a developer wants to keep using the same terminal for other tasks ( such as Git commands, testing, etc ).
  • When working on a cloud server via SSH and want to disconnect while keeping the app running.

Using & in Unix /Linux/MacOS

If you are using unix-based systems like Linux or macOS

  • Open your terminal.
  • Navigate to the directory where your package.json is located.
  • Run the following command:
npm start &
npmstart
npm run start &
  • It means & at the end tells the terminal to run the command in the background.
startingdevelopingserver2
starting development server
  • After running the command , we will get something like this:
[1] 12345
  • This means our process is running in the background , and 12345 is the process ID (PID). Now it allows to keep using the terminal while our server is running.

Stopping the Background Process:

If you want to stop the server , you can use kill commands followed by PID.

kill 12345
kill2

Using pm2 for Advanced Control

For more advanced process management , we have pm2 , a popular process manager for node.js applications , pm2 does a lot more than just background running your process -- it can automatically restart your app if it crashes , manage logs and even handle clustering.

  • Install pm2 globally:
npm install -g pm2
  • Then navigate to your project directory and start the app:
pm2 start npm -- start
  • This runs npm start in the background and give additional information like viewing logs, restarting the app or even setting it to auto-start on reboot.

Managing app using pm2:

pm2 is a powerful tools and it's good to manage multiple apps or need robust control over our node.js processes.

  • To check status of your app at any time
pm2 list
  • To stop the app
pm2 stop <process-id>
  • To see logs:
pm2 logs
pm2logs3

Conclusion

Running npm start / npm run start in the background can drastically improve the workflow , whether a solo developer or working in a large team .It help to free up the terminal and let us to focus on what matters the most -- writing code , not managing windows or terminal sessions.


Similar Reads