This guide explains how to create a Windows service for a Node.js application using the node-windows library.
- Node.js and npm installed on your system
- Administrative privileges (run commands as Administrator)
- Project directory containing your
index.jsfile
- Open PowerShell or Command Prompt as Administrator
- Navigate to your project directory:
cd Play-sound-on-webhook-alert
- Install the package:
npm install node-windows
Create create-service.js in your project root with this content:
const { Service } = require('node-windows');
const svc = new Service({
name: 'WebhookAlertService',
description: 'Node.js webhook service',
script: 'C:\\absolute\\path\\to\\your\\index.js', // Use actual path
nodeOptions: [
'--harmony',
'--max_old_space_size=4096'
]
// Optional: Add logging configuration
// logpath: 'C:\\path\\to\\logs',
// env: { name: 'NODE_ENV', value: 'production' }
});
svc.on('install', () => {
console.log('Service installed');
svc.start();
});
svc.on('start', () => console.log('Service started'));
svc.install();Important:
- Replace
C:\\absolute\\path\\to\\your\\index.jswith your actual file path - Use double backslashes (
\\) in paths nodeOptionsare optional and can be modified/removed
Run the installation script:
node create-service.jsSuccessful output should show:
Service installed
Service started
- Press
Win + R, typeservices.msc - Locate WebhookAlertService in the list
- Check status should be "Running"
sc query WebhookAlertServicenet start WebhookAlertService
net stop WebhookAlertServiceCreate remove-service.js:
const { Service } = require('node-windows');
const svc = new Service({
name: 'WebhookAlertService'
});
svc.on('uninstall', () => console.log('Service removed'));
svc.uninstall();Run removal script:
node remove-service.js- Logging: Uncomment
logpathin service config to store logs - Environment Variables: Add via
envproperty in service config - Automatic Restart: Service automatically restarts on failure
- Dependencies: Specify other services using
dependenciesarray
- No external tools required
- Full programmatic control
- Native Windows service integration
- Easy installation/removal process
- Access Denied: Always run commands as Administrator
- Path Errors: Verify file paths use double backslashes
- Service Not Starting: Check Windows Event Viewer for errors
- File Permissions: Ensure service account has file access rights