4

I wrote a c++ module using n-api, compiled it with cmake-js and now want to use it in my electron-vue app. If I use the module in a project without electron-vue it works. But when I try to use it in my electron-vue app I'm always getting this error:

 App threw an error during load
 TypeError: Cannot read property 'indexOf' of undefined
      at Function.getFileName (D:\temp\test2\node_modules\bindings\bindings.js:178:16)
      at bindings (D:\temp\test2\node_modules\bindings\bindings.js:82:48)
      at eval (webpack:///./src/main/index.js?:28:67)
      at Module../src/main/index.js (D:\temp\test2\dist\electron\main.js:3822:1)
      at __webpack_require__ (D:\temp\test2\dist\electron\main.js:21:30)
      at eval (webpack:///./src/main/index.dev.js?:11:1)
      at Object../src/main/index.dev.js (D:\temp\test2\dist\electron\main.js:3810:1)
      at __webpack_require__ (D:\temp\test2\dist\electron\main.js:21:30)
      at eval (webpack:///multi_./src/main/index.dev.js_./src/main/index.js?:1:1)
      at Object.0 (D:\temp\test2\dist\electron\main.js:3880:1)

I'm using bindings like so:

const colorBalance = require('bindings')('colorBalance');

I have tried to define my module as external according to this but it didn't solve the problem:

// vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      externals: ['NameOfMyModule']
    }
  }
}
groboter
  • 81
  • 2
  • 6

4 Answers4

1

Most probably you are trying to apply method 'indexOf' to variable which isn't defined yet. Take a look at docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf . Check if your variable is defined somewhere and it should be an array type.

webprogrammer
  • 2,393
  • 3
  • 21
  • 27
  • 2
    Thanks, but the error comes from the node-bindings module and not from my own code. The error happens here bindings.js:178:16 (first at... in the error message) seems that fileName is undefined but I don't know how to solve this. Everything works fine in my test app without electron-vue. – groboter Jan 23 '20 at 12:29
  • 1
    It's looks like this is the same issue as here https://github.com/TooTallNate/node-bindings/issues/54 – webprogrammer Jan 23 '20 at 12:30
  • To be true: I don't understand if it is the same issue. – groboter Jan 25 '20 at 14:17
0

In the meantime I've tried to import the module without bindings:

const colorBalance = require('../../build/Release/colorBalance.node');

Then I getting a new error:

Error: Cannot open D:\temp\test2\build\Release\colorBalance.node: Error: Module did not self-register.
      at Object.eval (webpack:///./build/Release/colorBalance.node?:1:155)
      at eval (webpack:///./build/Release/colorBalance.node?:2:30)
      at Object../build/Release/colorBalance.node (D:\temp\test2\dist\electron\main.js:97:1)
      at __webpack_require__ (D:\temp\test2\dist\electron\main.js:21:30)
      at eval (webpack:///./src/main/index.js?:28:20)
      at Module../src/main/index.js (D:\temp\test2\dist\electron\main.js:3833:1)
      at __webpack_require__ (D:\temp\test2\dist\electron\main.js:21:30)
      at eval (webpack:///./src/main/index.dev.js?:11:1)
      at Object../src/main/index.dev.js (D:\temp\test2\dist\electron\main.js:3821:1)
      at __webpack_require__ (D:\temp\test2\dist\electron\main.js:21:30)

I've rebuild electron using electron-rebuild. I've read about win_delay_load_hook here and tried this but the error doesn't disappear.

groboter
  • 81
  • 2
  • 6
0

I've solved my problem by changing the build chain from cmake to gyp. Compiling with gyp everything works fine.

groboter
  • 81
  • 2
  • 6
0

@grobotor, regarding the self-register issue. Please see these resources :)

TLDR; I discovered my issue was due to this in the bindings.gyp

"sources": [ ],

This was causing the error "Error: Module did not self-register" when I was attempting to run autotests on Linux (as module is only built for mac) https://github.com/codebytere/node-mac-permissions/issues/23

Regarding your original issue, I don't think electronBuilder externals is where that property should be since it's a packager. The externals needs to configure the bundler, such as with webpack's externals

Example of my configuration:

// Bundle all deps when building dist (except native modules), otherwise streamline development by just using local node_modules dir
externals: packDistributable ? [{
    permissions: "node-mac-permissions"
}] : [nodeExternals()],