Search code examples
javascriptnode.jsservicevbscript

Windows - can't run a node.js app as a background service with npm-windows?


I am trying to use node-windows to run my script as a background service.

I installed node-windows with the global flag, and then linked.

service.js:

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

console.log("entered");

// Create a new service object
var svc = new Service({
  name: "myService",
  description: "my service",
  script:
    "C:\\path-to-my-script\\example.js",
});

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

// Listen for the "start" event and let us know when the
// process has actually started working.
svc.on("start", function () {
  console.log(
    svc.name + " started!\nVisit http://127.0.0.1:3000 to see it in action."
  );
});

svc.on("error", function () {
  console.log(svc.name + " error occured");
});

svc.install();

console.log(" completed");

example.js:

var http = require("http");

const hostname = "127.0.0.1";
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader("Content-Type", "text/plain");
  res.end("Hello World");
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

After node service.js, I receive the following error: Windows Script Host: There is no script engine for file extension ".vbs"

I already tried the following:

  • in cmd: assoc .vbs=VBSFile

Still nothing happens.


Solution

  • The problem was that the default program for handling .vbs scripts was set to Notepad++, instead of Microsoft Windows Based Script Host. (You can see yours under Settings/Default apps/Choose default apps by file type).

    To revert it back , I found this as the only solution.