1

There's already a thread on this subject (here), but it doesn't answer my situation (most of the answers make suggestions for alternatives to node-windows instead of addressing why the service it creates stops running).

Like the title says, I used the node-windows package to create a service (run my node app script). It runs locally but when I install it on a windows 2012 server, the service stops after a few seconds after starting.

Here are the errors found in Event Viewer:

  • Starting D:\Program Files\nodejs\node.exe --harmony "D:\Program Files\otherApps\create-windows-service-for-nodejs\node_modules\node-windows\lib\wrapper.js" --file "D:\Program Files\path\to\my\application\index.js" --log "Node.js Service Management API wrapper" --grow 0.25 --wait 1 --maxrestarts 3 --abortonerror n --stopparentfirst undefined
  • Service started successfully.
  • Starting D:\Program Files\path\to\my\application\index.js
  • D:\Program Files\path\to\my\application\index.js stopped running.
  • Restarted 1250 msecs after unexpected exit; attempts = 1
  • D:\Program Files\path\to\my\application\index.js stopped running.
  • Restarted 1562.5 msecs after unexpected exit; attempts = 2
  • Child process [5800 - D:\Program Files\nodejs\node.exe --harmony "D:\Program Files\otherApps\create-windows-service-for-nodejs\node_modules\node-windows\lib\wrapper.js" --file "D:\Program Files\path\to\my\application\index.js" --log "Node.js Service Management API wrapper" --grow 0.25 --wait 1 --maxrestarts 3 --abortonerror

Here's my implementation of node-windows:

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

var svc = new Service({
  name:'Node.js Service Management API',
  description: 'The nodejs.org service management api.',
  script: 'D:\\Program Files\\nodeApps\\service-management-api\\server\\server.js'
});

svc.on('install',function(){
    svc.start();
    console.log('Install complete');
    console.log('The service exists: ', svc.exists)
  });
  

  svc.install();

Any suggestions on how to fix this service so it stays 'on'? Is it my implementation of node-windows? Or perhaps some windows server 2012 configuration issue?

Thank you!

208_man
  • 1,440
  • 3
  • 28
  • 59

1 Answers1

1

It's not the most satisfying resolution, but we solved it by simply removing the entire node-windows application and starting over from scratch. It now works. We also nested it within the actual node app that we are automating with this service, but I don't think that has anything to do with the fact that it now works. Here is the code in it's final state:

var Service = require('node-windows').Service;
 
// Create a new service object
var svc = new Service({
  name:'Node.js Service Management API',
  description: 'The nodejs.org service management api.',
  script: 'D:\\Program Files\\nodeApps\\service-management-api\\server\\server.js'
});
 
// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
});
 
svc.install();

We also created a separate 'uninstall' module which is exactly the same except the 'svc.on' function takes in as its first argument 'uninstall' instead of 'install'.

Hope this helps someone if they find themselves in the same situation.

208_man
  • 1,440
  • 3
  • 28
  • 59
  • Hi, how do you call your script once its running as windows service? – pratRockss Apr 22 '20 at 08:30
  • Hello @PratikNelge. Are you asking how to *manually* run the script (like in a dev environment) after setting up this module? If so, just stop the service and run the node process like you would any node process from within your terminal / command line. – 208_man Apr 22 '20 at 18:01