繁体   English   中英

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

[英]node module version conflict when installing modules for electron

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

所以我从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.安装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/

postinstall上进行electron-rebuild

根据您正在进行的操作,您可以使用电子重建serialport 重建为已安装的electron版本。

为此:

npm install --save-dev electron-rebuild

$(npm bin)/electron-rebuild                 # Mac and Linux.

.\node_modules\.bin\electron-rebuild.cmd    # Windows.

因为在执行npm安装后我一直忘记这样做(以及帮助其他下载项目的人),我将以下两个脚本添加到package.json

"scripts": {
  "start": "electron .",

  "postinstall": "electron-rebuild",    
  "electron-rebuild": "electron-rebuild"
},

执行npm install后, postinstall将自动运行,因此在典型安装完成后,您将看到带有electron-rebuild的控制台日志消息,它将自动重建serialport以及您拥有的任何其他本机库到electron版本。 这意味着您甚至不必考虑继续进行electron-rebuild 👍

要手动重新运行electron-rebuild只需使用npm run electron-rebuild运行它。

Easy-peezie,柠檬压榨!

使用内容创建文件.npmrc

runtime = electron
target = 1.7.5
target_arch = x64
disturl = https://atom.io/download/atom-shell
export npm_config_runtime=electron
export npm_config_build_from_source=true

打开另一个终端并运行npm install [yourpackage]

请记住 ,一些新软件包将安装最高电子版本(目标),因此请保存自己一些头痛/背痛并使用npm或github页面上陈述的当前版本更新您的target =

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM