2


I am currently working on an Electron-App using NPM that unzips a file to a selected directory. An Installer basically. Now when I build the application it still works fine. I simply start the setup.exe in my win-unpacked folder and everything goes down smoothly. Now when I move the win-unpacked folder to a different directory my app runs fine but when it starts the unzipping process it throws the following error:

enter image description here

I have noticed, that the first file-path displayed (for some reason) doesnt use utf8, but all the others are being displayed correctly (should be an ä). Also I have allready tried deleting the node_modules folder and then running npm i to reinstall them. Still having the same issue.

The following is the code that starts the unzipping process:

const path = require('path');
const ipcRenderer = require('electron').ipcRenderer;
const Unzip = require('./unzip');
const os = require('os');
const fs = require('fs');

$('#information_next').click(function () {
   var extractPath = $('#input_select').val();
   let filepath;
   const platform = os.platform();
   const nodeName = platform == 'win32' ? 'wcd.node' : (platform == 'darwin' ? 'mcd.node' : 'lcd.node');
   let customData = require("bindings")(nodeName);
   let zip = h2a(customData.customData());
   if(os.platform() == 'darwin') {
      filepath = path.join(__dirname, '..', '..', '..', '..', 'ZIP.zip');
   } else {
      filepath = path.join(__dirname, '..', '..', 'ZIP.zip');
   }
   var xPath = path.join.apply(null, extractPath.split('\\'));
   var unzip = new Unzip(filepath, xPath.toString());
   unzip.extract(extractPath, zip, 100, (percentage) => {

      // Code for Progressbar

      finish = true;
      setTimeout(function () {
         $('.main_wrapper').addClass('hidden');
         $('.main7').removeClass('hidden');
      }, 1500);
   }).catch((e) => {
      $('.main6').addClass('hidden');
      $('.main_install_error').prop('hidden', false);
   });
});

Here I am using the bindings module to require a .node file which passes a string to my app. And this seems to be the module that causes the Error.

I have been trying to resolve this for hours now, but nothing I've found online works. Also I couldn't find anyone who had the same Error that I have. Any suggestions on how to fix this would be appreciated.

Greetings
Matt.S

EDIT:

I might have just figured out the main problem. The bindings module contains a function that looks for the module root. This path is displayed in the first line of the ERROR. However since this app has allready been built, all the source code is inside of an app.asar file. Bindings can't seem to distinguish between the .asar file and an ordinary folder. So even tho the path is correct, it doesn't work. The reason why it worked in the original win-unpacked is because bindings (if it cant find the module-root) moves up througth the directory until it finds the root. And since the original win-unpacked folder is inside my project directory bindings uses the un-built module-root.

1 Answer 1

2
+50

I was able to reproduce this error with the bindings module.

It seems to have alot of problems with the electron framework which can cause such behaviour.

The 'root directory' Problem

You answered this yourself with your edit of the original question, but for the sake of delivering a complete answer, I am including this too

I simply start the setup.exe in my win-unpacked folder and everything goes down smoothly. Now when I move the win-unpacked folder to a different directory my app runs fine but when it starts the unzipping process it throws an error.

This has an interesting reason. Since your dist directory (the build destination of your project) is inside your working project, the bindings module assumes your working directory is the root directory of your built app. Thus it is able to resolve the path to your module and everything works fine. Once your built app is placed somewhere else, the bindings module isn't able to find the root directory of your app and throws the error you linked.

The 'file://' Problem

Another problem of the bindings module is handling paths with the 'file' protocol.

Someone already went ahead and created an issue (+ a pull request) for this problem, so you could modify your local installation of this module, even though I would discourage taking such actions.

My personal advice:

The current state of the bindings module makes it unattractive for use together with the Electron framework. I heard it even has problems handling umlauts properly, so your best bet in your specific situation is to get rid of it. You could make a small local module which wraps your .node binary and makes it as easy to require as all your other node modules.

This is an amazing article about creating your own module.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.