Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Electron protocol handler not working on windows

I’m trying to register a protocol handler using app.setAsDefaultProtocolClient and I’ve got it working fine on macOS but on windows 10 I get a dialog saying

Error launching app  
Unable to find Electron app at 'C:\Program Files(x86)\Google\Chrome\Application\60.0.3….. Ect
Cannot find module 'C:\Program Files(x86)\Google\Chrome\Application\60.0.3….. Ect

Is it right that it’s looking in Chrome\Application folder? I get the same error if I run with npm start or from a packaged app using electron-packager.

Is there something i’m missing that i need to configure for windows? Like the plist on mac? I’ve been looking round but can’t seem to find anything. let me know any info i can add to help.

like image 689
znap026 Avatar asked Aug 08 '17 14:08

znap026


1 Answers

I had the same problem: the protocol handler doesn't find the location of the app in development environment on Windows. Everything works on OSX, and Windows only when packaged. The fix here is to manually supply the path to your app when registering the protocol.

Originally, I had something like this, which worked on OSX and packaged.exe on Windows:

if(!app.isDefaultProtocolClient('app')) {
  app.setAsDefaultProtocolClient('app');
}

Here's the fix that corrected the path problem for developing on Windows:

// remove so we can register each time as we run the app. 
app.removeAsDefaultProtocolClient('app');

// If we are running a non-packaged version of the app && on windows
if(process.env.NODE_ENV === 'development' && process.platform === 'win32') {
  // Set the path of electron.exe and your app.
  // These two additional parameters are only available on windows.
  app.setAsDefaultProtocolClient('app', process.execPath, [path.resolve(process.argv[1])]);        
} else {
  app.setAsDefaultProtocolClient('app');
}

I had my project setup so that process.env.NODE_ENV would tell me if I'm in development environment or not. If you're in development environment, you want to pass in two additional parameters to app.setAsDefaultProtocolClient. The first argument, of course, is the protocol you want to register, the second argument should be the path to the electron executable. process.execPath is the default value, and should evaluate to /path/to/your/project/node_modules/electron/dist/electron.exe or similar.

The third argument is an array of arguments you want to run electron.exe with. In my case, I want to run my app, so I pass in the path wrapped in an array []. process.argv[1] is simply a way to get the path of the dev app, which should evaluate to /path/to/your/project/dist/electron/main.js or similar.

For more information: https://electronjs.org/docs/api/app#appsetasdefaultprotocolclientprotocol-path-args

like image 162
alan850627 Avatar answered Oct 17 '22 18:10

alan850627