Trying to run a node server as windows service on azure vm

245 Views Asked by At

I have build a node server and have been testing it periodically but running from powershell. Right now I am trying to use node-windows package to run the node server as a service, so that the server runs continuously. I get the following error while trying to start the service
enter image description here

When saw the events viewer I get the following error enter image description here

Here is the code for the service creation:

var Service = require('node-windows').Service;
 
// Create a new service object
var svc = new Service({
  name:'dlserver-9',
  description: 'This is dlserver',
  script: 'index.js',
  allowServiceLogon: true
});
svc.install();

The service starts but the next second it terminates automatically when viewed in the events viewer. I am running the node server on windows server 2019 os on azure vm.

1

There are 1 best solutions below

0
On

If you do not mind running it as a Window Service, you can use nssm to run it as Window Service.

Below are some of the teething problem you may encounter and how you can resolve it:

  1. If the application is unable to run properly in Window Service, you can create a batch file to run it. Example, create a file name: startdlserver9.bat and in this file write: npm start, or whatever command you use to run your node application in command prompt.

  2. If nssm cannot install it in window service. Sometime is because your batch file is not in the same folder as your application home page, the folder where you run your npm start.

  3. Also if have problem removing a window service, just ensure you close the Window Services screen, before you run the nssm remove command.