微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 安装电子模块时的节点模块版本冲突

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

所以我从github快速入手,跑了

npm install && npm start

随着这个轻松工作,我尝试安装和运行serialport

npm install serialport

使用测试文件安装并运行正常,我尝试将两者结合起来并在index.html文件中放置require(‘serialport’).有了这个,我得到这个错误

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,为什么我会收到此错误.

系统OS信息:

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

解决方法:

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

在你的应用程序目录中(package.json所在的目录),

1.安装电子重建

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/

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐