Hello friends , today I explain you How to Run NodeJS Application as a Windows Service or background Service . Node.js has gained immense popularity in recent years due to its ability to develop fast and scalable applications.
also we learn about How to deploy node js application on Windows server without IIS with the help of node window service.
However, when it comes to deploying and managing these applications in production, one of the most significant challenges is ensuring they run continuously as a service on a Windows operating system.
In this article, we will discuss how to install a node.js application as a Windows service, including why it is essential and what tools to use to make the process easy and seamless.
There are many library available for window services and we use the node-window in this article .
Introduction
Normally, we use the node command on the command line to launch a node.js application after it has been developed. But if we want to deploy it in a real-world setting, we have to make sure it runs continually and automatically.
Installing it as a Windows service is one method to accomplish this. When no user is signed in, a programme known as a Windows service continues to operate continually in the background.
Now, if Windows restarts or the service crashes for any reason, the service may be configured to automatically restart.
It can be set up to restart immediately if it fails, and it launches automatically when the machine turns on.
What is a Windows Service?
Windows services are persistent background tasks that continue to run even when no users are logged in. Windows services are designed to run automatically and provide the essential functionality to the operating system or applications.
They can be used for a number of activities, including as executing server software, keeping an eye on system events, and performing set tasks. They resemble UNIX daemons in certain ways.
Increased security, automatic launch, and automatic recovery are advantages of executing an application as a service.
An programme that is launched as a service is hidden from the user and operates in the background.
The service may also be configured to start up automatically when the system starts and to resume when something goes wrong.
Why install a node.js application as a Windows service?
There are several reasons why you might want to install a node.js application as a Windows service. Here are a few:
- Continuous running: By installing the application as a Windows service, we ensure that it runs continuously, even if the computer reboots or crashes. This is critical for production environments where downtime can be costly.
- Automatic startup: When a software is installed as a Windows service, it may be configured to run automatically when the computer turns on. As a result, the application will always be available even if the server is restarted.
- Easy management: Windows services are easy to manage using the built-in Windows Service Manager or third-party tools. we can start, stop, and restart the service with a click of a button.
Prerequisites for installing a node.js application as a Windows service
Before you can install a node.js application as a Windows service, we need to have the following prerequisites:
- Node.js: Node.js must be installed on the computer where the service will be installed. We may get the most recent version from the official Node.js website.(https://nodejs.org/en/).
- npm: we need to have npm (Node Package Manager) installed on the computer as well. npm comes bundled with Node.js, so we should have it already installed.
- Windows OS: we need to have a Windows operating system installed on the computer where you want to install the service. The steps to install the service might differ slightly depending on the version of Windows you are using.
Steps to install a node.js application as a Windows service
Now that you have the prerequisites in place, let’s take a look at the steps to install a node.js application as a Windows service.
Step 1: Install the necessary modules
The first step in deploying the programmes as a Windows service is to install the necessary components. At the command prompt, go to the directory where your node.js application is located. Execute the following command after that:
- Open the Command Prompt or PowerShell window.
- Navigate to the directory where you want to install node-windows.
- Run the following command to install node-windows:
npm install -g node-windows
npm link node-windows
Step 2 : Create node js app
In second step we create sever.js file, In this file we create a simple hello world program
const express = require("express")
const app = express();
app.get("/",(req, res)=>{
res.send("Server is running good");
}
app.listen(3000,(req, res)=>{
console.log("Server is running on 3000");
})
Step 2: Create a Windows service script
Next, we need to create a script that will register our node.js application as a Windows service. Create a new file called `service.js
`
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'Simple hello world',
description: 'To test alternative of nssm',
//your project file destination in our case we use server.js
script: 'C:\\temp\\server.js',
nodeOptions: [
'--harmony',
'--max_old_space_size=4096'
]
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
svc.start();
});
svc.install();
after that we run this service file following this command :
node service.js
when run this file many prompts are open you simple click on yes button after we successfully create window services and in cmd we see this type of output
{
loc: 'winsw.js ~line 77',
xml: [
{ id: 'simplehelloworld.exe' },
{ name: 'Simple hello world' },
{ description: 'To test alternative of nssm' },
{ executable: 'C:\\Program Files\\nodejs\\node.exe' },
{ argument: '--harmony' },
{ argument: '--max_old_space_size=4096' },
{
argument: 'C:\\Users\\username\\AppData\\Roaming\\npm\\node_modules\\node-windows\\lib\\wrapper.js'
}
],
config: {
name: 'Simple hello world',
id: 'simplehelloworld.exe',
nodeOptions: [ '--harmony', '--max_old_space_size=4096' ],
script: 'C:\\Users\\username\\AppData\\Roaming\\npm\\node_modules\\node-windows\\lib\\wrapper.js',
scriptOptions: '',
wrapperArgs: [
'--file',
'C:\\temp\\server.js',
'--scriptoptions=',
'--log',
'Simple hello world wrapper',
'--grow',
0.25,
'--wait',
1,
'--maxrestarts',
3,
'--abortonerror',
'n',
'--stopparentfirst',
undefined
],
description: 'To test alternative of nssm',
logpath: null,
env: undefined,
execPath: null,
logOnAs: {
account: null,
password: null,
domain: 'TEGBLRCPU097',
},
workingdirectory: 'G:\\inetpub\\wwwroot\\basic-node-app',
stopparentfirst: undefined,
stoptimeout: 30,
logmode: 'rotate',
logging: undefined,
allowServiceLogon: undefined
}
}
After that you simple type W+R and type service you will see you window service is with the name of “simplehelloworld.exe”
If you want to uninstall node window services first create simple create a js file with the name of `uninstallservice.js`
var Service = require('node-windows').Service;
// Create a new service object
var svc = new Service({
name:'Simple hello world',
description: 'To test alternative of nssm',
//your project file destination in our case we use server.js
script: 'C:\\temp\\server.js',
nodeOptions: [
'--harmony',
'--max_old_space_size=4096'
]
});
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('uninstall',function(){
console.log('Uninstall complete.');
console.log('The service exists: ',svc.exists);
});
svc.uninstall();
to run this file we need to run command node uninstallservice.js
after that command your window process is deleted .
Troubleshooting common issues
If you encounter any issues while installing or running the node.js service as a Windows service, here are some common troubleshooting steps:
- Make sure that you have administrative privileges on the system.
- Check the event logs for any errors or warnings related to the service.
- Make sure that the service dependencies are installed and running.
- Check the service log files for any errors or warnings.
- Verify that the service log-on credentials are correct and have the necessary permissions.
Conclusion
In this article, we have discussed the steps involved in installing a node.js application as a Windows service. We have covered topics such as creating a service using node-windows, configuring service properties, managing services, and troubleshooting common issues. By following these steps, you can run your node.js applications as Windows services and enjoy the benefits of automatic startup, automatic recovery, and improved security.
FAQs
Can Node.js run as a service?
Yes, Node.js can be run as a service on various operating systems, including Windows, Linux, and macOS. This allows Node.js applications to be automatically started and stopped with the system, and provides more robust monitoring and management capabilities.
How do I run a Node.js application on windows?
To run a Node.js application on Windows, you’ll need to install Node.js first. Once Node.js is installed, you can open a command prompt or PowerShell window and navigate to the directory containing your application’s files. From there, you can use the node
command followed by the name of your main application file (e.g. node app.js
) to start the application.
How to install a Node.js service?
To install a Node.js service on Windows, you can use a third-party tool like NSSM (Non-Sucking Service Manager) or Windows Service Wrapper. These tools allow you to create a Windows service from your Node.js application, which can then be managed like any other Windows service.
How do I run a Node.js app as a background service?
To run a Node.js app as a background service, you can use a process manager like PM2 or Forever. These tools allow you to start, stop, and monitor Node.js processes, and can automatically restart them if they crash or fail.
create windows installer for node js application
To create a Windows installer for a Node.js application, you can use a tool like Electron Builder or NSIS (Nullsoft Scriptable Install System). These tools allow you to create a customized installer that includes your Node.js application and any required dependencies, and can also configure system settings or install additional software if needed.
How to deploy node js application on Windows server without IIS
To deploy a Node.js application on a Windows server without IIS, you can use a reverse proxy server like Nginx or Apache. These servers can be configured to pass incoming requests to your Node.js application, and can also handle SSL termination, load balancing, and other advanced features. Alternatively, you can use a cloud-based platform like Azure App Service or AWS Elastic Beanstalk, which provide a fully managed environment for deploying and scaling Node.js applications.
Is it possible to configure a service to run on a specific port?
Yes, you can configure the application to run on a specific port by specifying the port number in the application code.
Can I run multiple node.js services on the same machine?
Yes, you can run multiple node.js services on the same machine as long as they are configured to use different ports and do not conflict with each other.
What happens if the service encounters an error?
The service will stop running and may display an error message. You can configure the service to restart automatically if it encounters an error.