NodeJS app as windows service packed in .exe

210 Views Asked by At

I'm creating NodeJS application which will be ran as windows service. Whole app should be packed as .exe so it can be ran on windows. Let's take node-windows package that I'm using and create basic example (service.js)

var Service = require('node-windows').Service;

// Create a new service object
var svc = new Service({
  name:'Hello World',
  description: 'The nodejs.org example web server.',
  script: 'C:\\path\\to\\helloworld.js',
  nodeOptions: [
    '--harmony',
    '--max_old_space_size=4096'
  ]
  //, workingDirectory: '...'
  //, allowServiceLogon: true
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});

svc.install();

This code is from documentation. As helloworld.js is script that will be runned, I will add just simple code example for it

console.log("Starting async function");

async function myAsyncFunction() {
  // Do some asynchronous work here, e.g.:
  await new Promise(resolve => setTimeout(resolve, 5000));
  console.log('Async work done!');
}

myAsyncFunction();

When I run this, windows service is restarted multiple times immediately without waiting for async code to finish (works fine when I run node service.js). What I can see in logs is "Starting async function" but it doesn't wait for async code to finish. Is it even possible to run async code as windows service?

0

There are 0 best solutions below