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

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

所以我从github中快速启动,跑了

npm install && npm start 

有了这个轻松工作,我试图安装和运行serialport

 npm install serialport 

安装并运行良好的testing文件,我试图把两者结合起来,把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) 

任何想法如何解决它? 我没有使用两个不同版本的节点,为什么我得到这个错误。

系统操作系统信息

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

当发生这种types的版本不匹配时,您可以select具有目标节点版本的电子分布或重buildnpm软件包。 由于Electron的发行版跳过了configuration了NODE_MODULE_VERSION 51(跳到v7.4.0)的Node v7.0.0,所以你将不得不重buildserialport软件包。

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

1.安装electron-rebuild

npm install --save-dev electron-rebuild

2.重build

./node_modules/.bin/electron-rebuild

或者,即使是更好的select – 从第一个地方设置环境variables。

 # 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的文档页面,了解使用本地节点模块的情况。 https://electron.atom.io/docs/tutorial/using-native-node-modules/

electron-rebuild postinstall

根据你在做什么,你可以使用电子重build来重buildserialport到你已经安装的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的控制台日志消息,它会自动重buildodbc和其他任何你需要的electron版本库。 这意味着你甚至不必考虑进行electron-rebuild 。 👍

要手动重新运行electron-rebuild只需运行npm run electron-rebuild

容易peezie,柠檬squeezie!

用内容创build文件.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 

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

请记住 ,一些新的软件包将安装最高的电子版本(目标),所以保存一些头痛/背痛,并更新您的target = npm或github页面上的当前版本。