gpt4 book ai didi

javascript - 为 Electron 安装模块时 Node 模块版本冲突

转载 作者:IT老高 更新时间:2023-10-28 21:59:33 25 4
gpt4 key购买 nike

我正在尝试制作一个从我的串行端口读取数据的 Electron 应用程序 (https://electron.atom.io/)。一般来说,我是网络技术的新手,我知道一些 javascript,但我是一个 c++ 人。

所以我从 github 中提取了他们的快速入门,然后运行 ​​

npm install && npm start

通过这个简单的工作,我尝试安装和运行串行端口

npm install serialport

安装并运行良好的测试文件后,我尝试将两者结合起来,并将 require('serialport') 放入 index.html 文件中。有了这个,我得到了这个错误:

Uncaught Error: The module '/home/user/Documents/Programing/Git/Arduino-mpu6050/electron-quick-start/node_modules/serialport/build/Release/serialport.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 51. This version of Node.js requires
NODE_MODULE_VERSION 53. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or`npm install`).
at process.module.(anonymous function) [as dlopen] (ELECTRON_ASAR.js:173:20)
at Object.Module._extensions..node (module.js:598:18)
at Object.module.(anonymous function) [as .node] (ELECTRON_ASAR.js:173:20)
at Module.load (module.js:488:32)
at tryModuleLoad (module.js:447:12)
at Function.Module._load (module.js:439:3)
at Module.require (module.js:498:17)
at require (internal/module.js:20:19)
at bindings (/home/user/Documents/Programing/Git/Arduino-mpu6050/electron-quick-start/node_modules/bindings/bindings.js:76:44)
at Object.<anonymous> (/home/user/Documents/Programing/Git/Arduino-mpu6050/electron-quick-start/node_modules/serialport/lib/bindings.js:3:35)

任何想法如何解决它?我没有使用两个不同版本的 Node,为什么会出现此错误。

系统操作系统信息:

Distributor ID: Ubuntu
Description: Ubuntu 16.04.2 LTS
Release: 16.04
Codename: xenial

最佳答案

当发生这种类型的版本不匹配时,您可以选择具有目标 Node 版本的 Electron 发行版或重建 npm 包。由于 Electron 的发行版跳过了使用 NODE_MODULE_VERSION 51 配置的 Node v7.0.0(并跳转到 v7.4.0),因此您必须重新构建 serialport 包。

在您应用的目录(package.json 所在的位置)中,

1.安装 electron-rebuild

npm install --save-dev electron-rebuild


<强>2。重建

./node_modules/.bin/electron-rebuild



或者,甚至是更好的选择 - 从一开始就设置环境变量。

# Electron's version.
export npm_config_target=1.6.1
# The architecture of Electron, can be ia32 or x64.
export npm_config_arch=x64
export npm_config_target_arch=x64
# Download headers for Electron.
export npm_config_disturl=https://atom.io/download/electron
# Tell node-pre-gyp that we are building for Electron.
export npm_config_runtime=electron
# Tell node-pre-gyp to build module from source code.
export npm_config_build_from_source=true
# Install all dependencies, and store cache to ~/.electron-gyp.
HOME=~/.electron-gyp npm install

查看 Electron 的文档页面,了解如何使用原生 Node 模块。 https://electron.atom.io/docs/tutorial/using-native-node-modules/

关于javascript - 为 Electron 安装模块时 Node 模块版本冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42616008/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com