Getting an unusual error: "browserWindowOrWebView.getWebContents is not a function" after packaging my electron app


#1

I’m new to using node.js, and this is the first desktop app that I am trying to make with electron, so please excuse my lack of experience in this subject.

First, I would like to give some background information that lead me to this error.
I was able to package my electron app successfully, before implementing electron-spellchecker. When I ran npm start, electron-spellchecker worked perfectly, but after running npm run build the stand alone app gave me this error “Module version mismatch. Expected 47, got 50”. I was able to fix this error after reading some posts by other users who had been experiencing a similar problem. After fixing this error when I ran npm run build, the console showed a new error which is the following error:
Uncaught TypeError:browserWindowOrWebView.getWebContents is not a function
I can’t seem to figure out how to resolve this error. I would appreciate any help that I receive.

Here is some other information that may be useful to know.
OS: MacOS Sierra 10.12.5
node -v: v5.12.0
npm -v: 3.8.6
Electron version: 1.6.11

Once again, thanks for the help in advance.


#2

The first step to resolving it is to figure out where in your code the bad function call is. Somewhere, there’s an attempted invocation of browserWindowOrWebView.getWebContents() in a scope where that function doesn’t exist. Either that reference needs to be changed, or it needs to be defined or pulled in through require() so that that bit of code can work.


#3

Thanks for replying so quickly, The console provides the location of the error as being:
…Contents/Resources/app/node_modules/electron-remote/lib/remote-event.js:84

I have pasted the code around line 84 of the remote-event.js file below:

  let type = onWebContents ? 'webcontents' : 'window';
  let id;
  if (onWebContents) {
if (ctorName === 'WebContents') {
  id = browserWindowOrWebView.id;
} else {
  id = ('webContents' in browserWindowOrWebView ? browserWindowOrWebView.webContents : browserWindowOrWebView.getWebContents()).id;
}
  } else {
id = browserWindowOrWebView.id;
  }

I’m not sure why the code ran perfectly when running npm start, but fails to work in the standalone app. Anyways, How would one go about finding the “bad function call”?


#4

That right there is the bad function call. Considering what you’ve said, it’s probably supposed to be a good one, but if it doesn’t have access to the function it needs at that part of the program, it won’t work. Where does browserWindowOrWebView get defined? Is there a clear progression from the definition to that block of code, where there’s no way that it could fail to appear when called on in that segment?


#5

It turns out that browserWindowOrWebView is not defined within the electron-remote.js file, but is just a parameter given to the following function:

function fromRemoteWindow(browserWindowOrWebView, event) { ... }

I’m not sure what is calling this function, but I believe it is the following:

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.fromRemoteWindow = fromRemoteWindow;

electron-spellchecker has electron-remote in its list f dependencies, so i did not code either of these two files, and thats making it difficult to understand where the problem is originating.


#6

electron-spellchecker invokes fromRemoteWindow in context-menu-listener.js, starting at line 31:

if (!contextMenuEvent) {
  windowOrWebView = windowOrWebView || remote.getCurrentWebContents();
  contextMenuEvent = fromRemoteWindow(windowOrWebView, 'context-menu', true).map(([x]) => x[1]);
}

If that’s getting called at a point where there isn’t a window or web view (which there isn’t if you’re currently building a packaged form of the app), then electron-remote doesn’t have the information it requires.

I feel like you might submit an issue at the electron-spellchecker repo with a link to your project and ask if anyone else has successfully used the packager with that package.


#7

Thanks for your help, I was able to resolve the issue after some troubleshooting.


#8

How did you solve it?