Skip to content

Instantly share code, notes, and snippets.

@MichaelTaylor3D
Last active May 8, 2023 12:59
Show Gist options
  • Save MichaelTaylor3D/790b95142ad79d0f0757caa7b30bbd87 to your computer and use it in GitHub Desktop.
Save MichaelTaylor3D/790b95142ad79d0f0757caa7b30bbd87 to your computer and use it in GitHub Desktop.

To install an executable (.exe) as a Windows service, you can use the node-windows package in conjunction with the install.js script. Here's an example of how to set up a script to install an executable as a Windows service:

  1. Install node-windows:
npm install -g node-windows
  1. Create a new JavaScript file (e.g., install-exe-service.js) in your project folder:
const Service = require('node-windows').Service;
const path = require('path');

// Define the executable you want to run as a service
const exePath = path.join(__dirname, 'your-executable.exe');

// Create a new service object
const svc = new Service({
  name: 'Your Service Name',
  description: 'A description for your service',
  exePath: exePath,
});

// Listen for the "install" event, which indicates the service is installed
svc.on('install', () => {
  console.log('Service installed successfully');
  svc.start();
});

// Listen for the "alreadyinstalled" event, which indicates the service is already installed
svc.on('alreadyinstalled', () => {
  console.log('Service is already installed');
});

// Listen for the "error" event, which indicates there was an error installing the service
svc.on('error', (err) => {
  console.error('Error:', err.message);
});

// Install the service
svc.install();

Replace 'your-executable.exe' with the path to the executable you want to run as a service, and set a name and description for your service.

  1. Open the Command Prompt or PowerShell as an administrator and navigate to your project folder:
cd path\to\your\project
  1. Run the install-exe-service.js script:
node install-exe-service.js

If the service is installed successfully, you should see the "Service installed successfully" message.

  1. Your executable should now be running as a Windows service. You can view, start, stop, or remove the service using the Windows Services Management Console (services.msc) or the sc command in Command Prompt or PowerShell.

To uninstall the service, follow the same steps mentioned in the previous answer, but replace the script property with the exePath property in the uninstall-windows-service.js script.

Here's the JavaScript code using node-windows to uninstall a Windows service, assuming you've created a service using the same package. Make sure to replace the appropriate values for serviceName and exePath.

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

// Define the service properties
const serviceName = 'YourServiceName';
const exePath = 'C:\\path\\to\\your\\executable.exe';

// Create the service object with the specified properties
const service = new Service({
  name: serviceName,
  description: 'Your service description',
  script: exePath,
});

// Event listener for the uninstall event
service.on('uninstall', function () {
  console.log('Uninstall complete.');
  console.log('The service exists: ', service.exists);
});

// Uninstall the service
service.uninstall();

To uninstall the service:

  1. Save the code above in a JavaScript file, e.g., uninstall-service.js.
  2. Open a command prompt with administrator privileges.
  3. Navigate to the folder where you saved the uninstall-service.js file.
  4. Run the command npm install node-windows to install the node-windows package if you haven't already.
  5. Execute the command node uninstall-service.js to uninstall the service.

The script will uninstall the specified service. You should see the output indicating that the uninstall is complete and the service no longer exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment