无法使用电子读取 /node_modules/bindings/bindings.js 处未定义的属性“模块”并安装串行端口

Posted

技术标签:

【中文标题】无法使用电子读取 /node_modules/bindings/bindings.js 处未定义的属性“模块”并安装串行端口【英文标题】:Cannot read property 'modules' of undefined at /node_modules/bindings/bindings.js with electron and install serialport 【发布时间】:2021-05-27 11:59:39 【问题描述】:
1.  npm install -g node-gyp
2.  npm install serialport -S
3.  npm install electron-rebuild -D
4. ./node_modules/.bin/electron-rebuild.cmd

然后,重建complete。

当我运行这个命令时:npm run electron:serve 我在检查浏览器电子时遇到错误,例如 this。

如果我输入bindings.js 代码,我可以找到error 在哪里:

nodePreGyp:
  'node-v' +
    process.versions.modules

这是我的 package.json 文件:

"scripts": 
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint",
    "electron:build": "vue-cli-service electron:build",
    "electron:serve": "vue-cli-service electron:serve",
    "postinstall": "electron-builder install-app-deps",
    "postuninstall": "electron-builder install-app-deps"
  ,
  "main": "background.js",
  "dependencies": 
    "axios": "^0.21.0",
    "bindings": "^1.5.0",
    "core-js": "^3.6.5",
    "echarts": "^5.0.0",
    "element-resize-detector": "^1.2.1",
    "element-ui": "^2.14.1",
    "serialport": "^9.0.7",
    "sockjs-client": "^1.5.0",
    "vue": "^2.6.11",
    "vue-i18n": "^8.22.4",
    "vue-router": "^3.2.0",
    "vuex": "^3.4.0",
    "webstomp-client": "^1.2.6",
    "ws": "^7.4.3"
  ,
  "devDependencies": 
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-plugin-vuex": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/eslint-config-prettier": "^6.0.0",
    "babel-eslint": "^10.1.0",
    "electron": "^7.1.9",
    "electron-rebuild": "^2.3.5",
    "electron-builder": "^20.19.2",
    "electron-devtools-installer": "^3.1.0",
    "eslint": "^6.7.2",
    "eslint-plugin-prettier": "^3.1.3",
    "eslint-plugin-vue": "^6.2.2",
    "node-sass": "^4.12.0",
    "prettier": "^1.19.1",
    "sass-loader": "^8.0.2",
    "svg-sprite-loader": "^5.2.1",
    "vue-cli-plugin-electron-builder": "~2.0.0-rc.5",
    "vue-template-compiler": "^2.6.11"
  ,
  "eslintConfig": 
    "root": true,
    "env": 
      "node": true
    ,
    "extends": [
      "plugin:vue/essential",
      "eslint:recommended",
      "@vue/prettier"
    ],
    "parserOptions": 
      "parser": "babel-eslint"
    ,
    "rules": 
  ,
  "browserslist": [
    "> 1%",
    "last 2 versions",
    "not dead"
  ],
  "__npminstall_done": false

您能告诉我问题出在哪里或帮助我更了解如何解决这个问题吗?

【问题讨论】:

node-gyp 取决于您的操作系统,您的问题似乎在处理这个......您的操作系统是什么?您是否验证了 node-gyp 的良好安装? 我使用 'node-gyp list' 命令,gyo info show ' gyp info 如果它以 ok 结束,gyp info 使用 node-gyp@7.1.2,gyp info 使用 node@14.15 .4 | win32 | x64' :) 我的操作系统是 win 10 ,x64 非常感谢,我正在期待您的重播。 @user15280394 你有没有想过这个问题?我有同样的问题,我认为它与串行端口有关 【参考方案1】:

如果您使用 VueJS 使用电子,请参阅 https://github.com/SimulatedGREG/electron-vue 已经构建了所有代码并且也很有帮助

第一次面对同样的问题,用 Electron 实现现有的 VueJS 项目 SerialPort 包

enter image description here

<template>
  <div id="wrapper">
    <h2>Hello world</h2>

    <button @click="listMyPorts">Show Ports</button>

    <pre v-if="msg.length">msg</pre>

  </div>
</template>

<script>
  const serialport = require('serialport')

  export default 
    name: 'landing-page',
    data: () => (
      msg: []
    ),
    methods: 
      async  listSerialPorts () 
        await serialport.list().then((ports, err) => 
          if (err) 
            this.msg.push(err.message)
            return
           else 
            this.msg.push('')
          
          console.log('ports', ports)
          this.msg.push(ports)

          if (ports.length === 0) 
            this.msg.push('No ports discovered')
          
        )
      ,
      listMyPorts () 
        let vm = this
        setTimeout(function listPorts () 
          vm.listSerialPorts()
          setTimeout(listPorts, 2000)
        , 2000)
      
    
  
</script>

<style scoped>
</style>

【讨论】:

以上是关于无法使用电子读取 /node_modules/bindings/bindings.js 处未定义的属性“模块”并安装串行端口的主要内容,如果未能解决你的问题,请参考以下文章

Ionic v4 Firebase:无法读取未定义的属性“电子邮件”

在Excel中编辑后,OpenXML无法读取电子表格数据

无法使用 JavaMail 读取 Outlook 邮件,而 Gmail 可以工作

无法使用电子读取 /node_modules/bindings/bindings.js 处未定义的属性“模块”并安装串行端口

错误:找不到要导入的文件或无法读取:引导电子邮件

无法从谷歌电子表格中读取