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:
Step-1: 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.
Step-2: 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.
Step-3: 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.
- Open the command prompt and make sure to run as Administrator.
- 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).
- 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)
- 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:
- 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:
- Now finally, you can initiate the new service with the command:
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.
This is a life saver!!
This saved me a bunch. thank you thank you thank you! 😀
m getting below error while starting “nssm start app”
app: Unexpected status SERVICE_STOPPED in response to START control.
Did you follow every step correctly? Maybe you missed something.
I had this happen too. Try changing the file names so everything matches before following the instructions.
This can happen when you do not use quotes around a path that contains spaces. Check that first.
Thanks for helping out.
This indeed a great article!
Just a question,
how to get list of all services created using nssm?
You can use the this Powershell command to get the list of services created using nssm:
Get-WmiObject win32_service | ?{$_.PathName -like '*nssm*'} | select Name, DisplayName, State, PathName
Thanks for the doc. Is there a way to run the nssm service at a specific time instead on machine boot?
Development on NSSM appears to have halted years ago. Is this still a viable solution with no support for bug fixes?
Fortunately, it’s still a viable solution and there are no bugs as of now. You can use it without any worry.
I am about the trial this in lab but have question on how to use if I have multiple Node scripts?
Can you name the service this is linked to the JS script? I have need to run two app simultaneously
I have figured this out in step 3 (app) – This is the Name that will appear in the Services listing and path to executable is the nssm.exe.
My JS appear is now working which is grand!!!
when i used node red using nssm as service , why i can not run execute file (ex:notepad.exe) using exec node?