【问题标题】:Error "Cannot read property 'indexOf' of undefined" binding n-api module错误“无法读取未定义的属性 'indexOf'”绑定 n-api 模块
【发布时间】:2020-05-09 13:13:34
【问题描述】:

我使用n-api 编写了一个c++ 模块,使用cmake-js 编译它,现在想在我的electron-vue 应用程序中使用它。如果我在没有electron-vue 的项目中使用该模块,它可以工作。但是当我尝试在我的 electron-vue 应用程序中使用它时,我总是会收到这个错误:

 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)

我像这样使用bindings

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

我尝试根据this 将我的模块定义为外部模块,但没有解决问题:

// vue.config.js
module.exports = {
  pluginOptions: {
    electronBuilder: {
      externals: ['NameOfMyModule']
    }
  }
}

【问题讨论】:

    标签: vue.js cmake electron n-api


    【解决方案1】:

    您很可能正在尝试将方法“indexOf”应用于尚未定义的变量。查看文档https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf。检查你的变量是否定义在某个地方,它应该是一个数组类型。

    【讨论】:

    • 谢谢,但错误来自节点绑定模块,而不是我自己的代码。错误发生在这里 bindings.js:178:16 (首先在...在错误消息中)似乎 fileName 未定义,但我不知道如何解决这个问题。没有电子vue,我的测试应用程序一切正常。
    • 看起来和这里github.com/TooTallNate/node-bindings/issues/54的问题相同
    • 说实话:我不明白这是否是同一个问题。
    【解决方案2】:

    与此同时,我尝试在没有bindings 的情况下导入模块:

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

    然后我得到一个新的错误:

    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)
    

    我已经使用electron-rebuild 重建了电子。我读过win_delay_load_hook here 并尝试过this 但错误并没有消失。

    【讨论】:

      【解决方案3】:

      我通过将构建链从 cmake 更改为 gyp 解决了我的问题。使用gyp 编译一切正常。

      【讨论】:

        【解决方案4】:

        @grobotor,关于自助注册问题。请查看这些资源:)

        TLDR;我在 bindings.gyp 中发现我的问题是由于这个原因

        "sources": [ ],
        

        当我尝试在 Linux 上运行自动测试时,这导致了错误“错误:模块未自行注册”(因为模块仅针对 mac 构建) https://github.com/codebytere/node-mac-permissions/issues/23

        关于您的原始问题,我认为 electronBuilder externals 不应该是该属性的位置,因为它是一个打包程序。 externals需要配置bundler,比如用webpack的externals

        我的配置示例:

        // 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()],
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2014-09-14
          • 1970-01-01
          • 2016-08-26
          • 2018-08-30
          • 2021-02-05
          • 2022-01-01
          • 2022-11-15
          • 2017-08-30
          相关资源
          最近更新 更多