coreybutler / node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
Other
2.83k stars 356 forks source link

Service install silently fails on Windows 11 #338

Open longzheng opened 1 year ago

longzheng commented 1 year ago

Issue: When using svc.install(); the service fails to install in Windows 11 without any warning or error.

How To Reproduce:

  1. Start with a blank Windows 11 environment (e.g. use "Windows Sandbox")
  2. Install node
  3. Create project with npm install node-windows
  4. Run sample service install script
    
    var Service = require('node-windows').Service;

// Create a new service object var svc = new Service({ name:'Hello World', description: 'The nodejs.org example web server.', script: 'C:\path\to\helloworld.js', nodeOptions: [ '--harmony', '--max_old_space_size=4096' ] //, workingDirectory: '...' //, allowServiceLogon: true });

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

svc.install();


**Expected Behavior:**
Service is installed.

Or, error is emitted.

**Actual Behavior:**
Service is not installed.

Even adding an error event handler, there is no error emitted.

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

svc.on('error', (err) => {
  console.log(err)
})

svc.install();

Desktop: Windows 11 22H2

image

Additional context: I believe the issue is related to the very old version of winsw.exe bundled with the project. https://github.com/coreybutler/node-windows/tree/master/bin/winsw

If I try to run winsw.exe directly (but providing it a sample .xml to install a service), it fails. However if I use a newer version of winsw.exe from https://github.com/winsw/winsw/releases/tag/v2.12.0 then the service is installed correctly.

I believe this PR attempting to upgrade WinSW https://github.com/coreybutler/node-windows/pull/268 might address this issue (EDIT: Nope, unfortunately that version of WinSW doesn't work either. I tested 2.12.0 directly and works https://github.com/winsw/winsw/releases/tag/v2.12.0.

longzheng commented 1 year ago

After a bit more debugging, turns out https://github.com/coreybutler/node-windows/pull/268 does work in Windows 11 however not in Windows 11's Windows Sandbox.

When installing the service in Windows Sandbox, I noticed this error in my wrapper.log file when trying to install the service.

2023-02-16 21:09:15,090 INFO  - Starting ServiceWrapper in the CLI mode
2023-02-16 21:09:16,028 FATAL - Unhandled exception
System.Management.ManagementException: Provider load failure 
   at System.Management.ManagementException.ThrowWithExtendedInfo(ManagementStatus errorCode)
   at System.Management.ManagementObjectCollection.ManagementObjectEnumerator.MoveNext()
   at WMI.WmiRoot.ClassHandler.Invoke(Object proxy, MethodInfo method, Object[] args)
   at winsw.WrapperService.Run(String[] _args, ServiceDescriptor descriptor)
   at winsw.WrapperService.Main(String[] args)

I confirmed by updating winsw.exe to v2.12.0 fixes installations in Windows Sandbox (and in Windows 11).

coreybutler commented 1 year ago

It does look like upgrading winsw will fix this. The new winsw.exe is 17MB though (instead of 58kb), so the project increases its footprint for each service significantly. People using this on mobile Windows devices may be impacted by such a change. So, if this were included, it would have to be a major change (potentially breaking for some). I don't have any metrics to know how many people are using this in a sandbox, so it's hard to tell whether this change benefits the project or opens a can of worms... especially considering #317.

longzheng commented 1 year ago

After a bit more testing, looks like we don't necessarily need WinSW-x64.exe which I believe is the one with .NET Core bundled. WinSW.NET461.exe seems to work in Windows 11 (and Windows 10) is 641KB, which is still larger but not as scary as 17MB.

I appreciate your plans for an entirely different solution but I think maybe bumping WinSW is a good interim solution otherwise this library's primary purpose silently fails.

asdf23 commented 8 months ago

This is silently failing for me in Windows 10. How can I upgrade this? Perhaps this is a new/different issue?