1.0.2 • Published 2 years ago

desktop-service v1.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

desktop-service

Run Node.js scripts as native services on Windows, Mac OS and Linux.

Usage

var Service = require('desktop-service');

var svc = new Service({
  name: 'Sample Service',
  description: 'Sample service running Node.js script.',
  script: require('path').join(__dirname, 'service.js'),
});

svc.on('install', function () {
  svc.start();
});

svc.install();

After the installation the service will be listed like any other service in the service management utility of the OS.

Sample Service listed in the Services utility on Windows

Environment Variables

Sometimes you may want to provide a service with static data, passed in on creation of the service. You can do this by setting environment variables in the service config, as shown below:

var svc = new Service({
  name: 'Sample service',
  description: 'Sample service running Node.js script.',
  script: require('path').join(__dirname, 'service.js'),
  env: [
    {
      name: 'HOME',
      value: process.env['USERPROFILE'], // service is now able to access the user who created its home directory
    },
    {
      name: 'TEMP',
      value: path.join(process.env['USERPROFILE'], '/temp'), // use a temp directory in user's home directory
    },
  ],
});

Uninstalling a Service

var Service = require('desktop-service');

var svc = new Service({
  name: 'Sample Service',
  script: require('path').join(__dirname, 'service.js'),
});

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

svc.uninstall();

Note that the uninstall process only removes process-specific files. It does not delete your Node.js script.