How to Run NodeJS Application as a Windows Service

Configurare noua (How To)

Situatie

In a development environment, it’s very easy to run your server using a simple command but in a production environment, your Node application needs to be up and running at all times. To run a Node application as a Windows service, you need to leverage another third-party tool. The best and the easiest one to configure in this case would be NSSM.

What does NSSM offer?

Once you install and configure NSSM for your Node.js application, you can be assured that your server will run forever as a background service.

Here are the most salient features of NSSM:

  • NSSM is a free and open-source software
  • It converts executables into Windows services
  • NSSM can manage the CPU affinity and process priority of the managed application
  • It acts as a Load balancer for your services
  • NSSM monitors the services at all times. Whenever there’s a failure or machine reboots, it will automatically start the services

How to run Node server as a Service using NSSM

Here are step-by-step instructions with screenshots on how to install and use NSSM to ensure the availability of your Node application server at all times:

Solutie

Pasi de urmat

Download NSSM

You can download the latest package of NSSM from HERE.

If you’re running Windows 10, you should download the latest pre-release build to avoid any problems.

NSSM download

Renaming and placing the NSSM folder in C: drive

It’s not a compulsory step but it’s better to rename the folder to a simple name and place it in the C: drive for better accessibility.

For this exercise we are going to name the folder “nssm” and move it to the main C: drive.

NSSM placement

Creating a Service for your Nodejs Application

The next step is to create a Windows service using NSSM for your Node application. Follow the procedure step-by-step and make sure to get the locations of the folders right.

  1. Open the command prompt and make sure to run as Administrator.
  2. Traverse to the path where nssm.exe is located. If you followed the previous step it should be located in C:\nssm\nssm\win64 (If your Windows is 4-bit).
    NSSM Location
  3. Assuming you have Nodejs installed in the default folder (C:\Program Files\nodejs), you need to enter the following command in the command prompt: (app is the service name that you want to assign for your Node application)
    NSSM install
  4. Now we need to set the App Directory for the Nodejs app that you need to run as service. Assuming the App is located at: D:\app Enter this command:
    NSSM App directory
  5. Once that’s done, you need to set the App parameters to the server.js file that needs to run every time. Here’s the command to do that:
    NSSM App Parameters
  6. Now finally, you can initiate the new service with the command:
    NSSM start app

And that is it! Your Nodejs app will now run as a background Windows service. If anything fails or your machine boots up, the application will launch automatically, ensuring maximum availability for your Node application.

Tip solutie

Permanent

Voteaza

(13 din 31 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?