错误:在渲染器中加载非上下文感知的本机模块:@serialport \ bindings \ build \ Release \ bindings.node'

问题描述

我是电子和串行端口的新手。我启动了一个简单的节点项目,只需安装了电子和串行端口,一切都很好,但是当我使用let serialport = require('serialport')时,会出现上述错误。我什至没有在index.html页面的javascript中使用第二行代码。 我已经尝试并搜索了很多解决方案,但找不到。 我什至在https://github.com/electron/electron/issues/18397

读到了它的原因
"dependencies": {
    "electron": "^9.2.0","serialport": "^9.0.1"
  }

如果我使用
app.alloWrendererProcessReuse = false
它给出以下错误

Uncaught Error: The module '\\?\D:\newutkal\node_modules\@serialport\bindings\build\Release\bindings.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 72. This version of Node.js requires
NODE_MODULE_VERSION 80. Please try re-compiling or re-installing
the module (for instance,using `npm rebuild` or `npm install`).

我也尝试过npm install,但同样的错误。 请以简单的步骤(如果可能,以编码方式)说明其解决方案。 感谢您对此主题的关注。 :)

解决方法

electron guide-installation 中的串行端口文档所写:

  1. 运行 npm install --save-dev electron-rebuild

2.将电子重建添加到您项目的 package.json 的安装钩子

3.运行npm install

如果你有 Windows 上的问题,请尝试:.\node_modules.bin\electron-rebuild.cmd