Skip to content

Simple example is not working on Win7 #3

Closed
synaptiko opened this Issue Mar 25, 2013 · 5 comments

3 participants

@synaptiko

I'm using following code:

// file install-as-service.js
var Service = require('node-windows').Service;
var package = require('./package.json');
var name = package.name;
var description = package.description;
var svc = new Service({
  name: name,
  description: description,
  script: "" + __dirname + "/index.js"
});

svc.on('install', function() {
  console.log('installed');
  return svc.start();
});

svc.on('error', function() {
  return console.log('error');
});

svc.on('alreadyinstalled', function() {
  return console.log('alreadyinstalled');
});

svc.install();
// file index.js
var http = require('http');
http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');

But when called node install-as-service.js I've got following error:

buffer.js:434
      throw new Error('Unknown encoding');
            ^
Error: Unknown encoding
    at Buffer.toString (buffer.js:434:13)
    at Object.fs.readFileSync (fs.js:236:33)
    at Object.module.exports.createExe (D:\git\test-server\node_modules\node-windows\lib\winsw.js:93:19)
    at Object.defineProperties.install.value (D:\git\test-server\node_modules\node-windows\lib\daemon.js:284:19)
    at Object.oncomplete (fs.js:297:15)

And when called again, output was:

alreadyinstalled

I don't see any testserver.exe file created under ./daemon, only testserver.xml file is here.

I'm using node version 0.8.10 and Win7 Professional SP1.

@coreybutler
Owner

Hmm, the core of this may be an issue with node itself. Can you run the example right out of the example folder, i.e. node .\example\install.js? I'm curious if that throws the same error (I suspect it will). The stack trace indicated the error source as the call to fs.readFileSync(). It's possible that upgrading node will eliminate this issue.

The alreadyinstalled message is happening because ./daemon exists. It only checks for the existence of the directory. It would make sense to add some more logic around that to detect whether it's a valid install or not. I'm going to put that on my to-do list.

If there is no .exe, then the service did not get registered. You should be able to remove the ./daemon directory for a clean start.

@synaptiko

Yes, if I run node .\example\install.js error is the same.

But after I installed the newest version of Node.js it started working. Thanks :-)

@synaptiko synaptiko closed this Mar 26, 2013
@coreybutler
Owner

Thanks for the follow up @synaptiko - I'll have to set the node.js version dependency to 10.1 (I'm going to reopen this as a reminder to do that in my next npm release)... if time allows, I'll see if I can make a workaround for older versions though too.

@coreybutler coreybutler reopened this Mar 26, 2013
@EesaJacobs

@coreybutler, I have a nodejs server that's still busy finding its feet. At times, some errors cause the server to break, example:
console.log(person.name) -> person is undefined so the server breaks...
In the scenario explained above, the node server running as a windows service using node-windows does not break, it still says "running" when viewing the services. The service does not automatically restart when it breaks. What can I do?

I was going to use node's "forever" module but that's stuffed up and hasn't been maintained for about a year...

I suppose a can do something on the "svc.error" event but it never appears to fire, please see below

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

// Create a new service object
var svc = new Service({
name:service_config.Name,
description: service_config.Description,
script: service_config.PathToJS,
abortOnError:true,
wait: 2,
grow: .5
});

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

//Some error?
svc.on('error', function() {
console.log("some error");
});

//svc already exists, remove svc -- dev
svc.on('alreadyinstalled', function() {
console.log('svc already installed');
// Uninstall the service.
svc.uninstall();
});

//event after remove svc
svc.on('uninstall',function(){
console.log('Uninstall complete.');
console.log('The service exists: ',svc.exists);
svc.install();
});

svc.install();

@coreybutler
Owner

The node script isn't auto-restarting because that's how it's being configured. If you want it to auto-restart, abortOnError should be set to false, i.e.

var svc = new Service({
name:service_config.Name,
description: service_config.Description,
script: service_config.PathToJS,
abortOnError:true,  // <--------- Change this to false
wait: 2,
grow: .5
});

Off the top of my head, I'm not sure why the process continues to show as running even when it no longer is. There is a wrapper node script in node-windows that is still running, even though the script died. Again, I'm not sure why this is happening in you specific case, but I am in the process of refactoring this... so my suggestion for now is to wait until the new code is ready. That will realistically be faster since I don't have a Windows 7 computer to test on at the moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.