Javascript 为电子安装模块时节点模块版本冲突
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42616008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
node module version conflict when installing modules for electron
提问by Ryan
I'm trying to make an Electron application (https://electron.atom.io/) that reads data from my serial port. I'm new to web technologies in general, I know some javascript, but I'm a c++ guy.
我正在尝试制作一个从我的串行端口读取数据的 Electron 应用程序(https://electron.atom.io/)。我是网络技术的新手,我知道一些 javascript,但我是一个 C++ 人。
So I pulled in their quick-start from github, ran
所以我从 github 中提取了他们的快速入门,跑了
npm install && npm start
With this easily working I tried to install and run serialport with
有了这个轻松工作,我尝试安装和运行串行端口
npm install serialport
With that installed and running fine with a test file, I tried to combine the two and put require('serialport')in the index.html file. With this in there I get this error:
使用测试文件安装并运行良好后,我尝试将两者结合起来并放入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)
Any ideas how to fix it? I'm not using two different versions of Node, why am I getting this error.
任何想法如何解决它?我没有使用两个不同版本的 Node,为什么会出现此错误。
System OS info:
系统操作系统信息:
Distributor ID: Ubuntu
Description: Ubuntu 16.04.2 LTS
Release: 16.04
Codename: xenial
回答by subwaymatch
When this type of version mismatch occurs, you can either choose an electron distribution with the target Node version or rebuild the npm package. Since Electron's distribution has skipped Node v7.0.0 which is configured with NODE_MODULE_VERSION 51 (and jumped to v7.4.0), you would have to rebuild the serialportpackage.
当出现这种版本不匹配时,你可以选择与目标 Node 版本相同的电子发行版,也可以重新构建 npm 包。由于 Electron 的发行版跳过了配置了 NODE_MODULE_VERSION 51 的 Node v7.0.0(并跳转到了 v7.4.0),因此您必须重新构建serialport包。
In your app's directory (where package.json is located at),
在您的应用程序目录中(package.json 所在的位置),
1. Install electron-rebuild
1.安装 electron-rebuild
npm install --save-dev electron-rebuild
npm install --save-dev electron-rebuild
2. Rebuild
2. 重建
./node_modules/.bin/electron-rebuild
./node_modules/.bin/electron-rebuild
Or, even a better option- set environment variables from the first place.
或者,甚至是更好的选择- 从一开始就设置环境变量。
# 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
Take a look at the Electron's documentation page for using native Node modules. https://electron.atom.io/docs/tutorial/using-native-node-modules/
查看 Electron 的文档页面,了解如何使用原生 Node 模块。 https://electron.atom.io/docs/tutorial/using-native-node-modules/
回答by Joshua Pinter
electron-rebuildon postinstall.
electron-rebuild上postinstall。
Depending on what you're doing, you can use electron-rebuildto rebuild serialportto the version of electronyou have installed.
根据您在做什么,您可以使用电子重建来重建serialport到electron您安装的版本。
To do so:
这样做:
npm install --save-dev electron-rebuild
$(npm bin)/electron-rebuild # Mac and Linux.
.\node_modules\.bin\electron-rebuild.cmd # Windows.
Because I kept forgetting to do this after doing an npm install (and to help others that downloaded the project), I added the following two scripts to package.json:
因为我在执行 npm install 后一直忘记这样做(并帮助其他下载该项目的人),所以我将以下两个脚本添加到package.json:
"scripts": {
"start": "electron .",
"postinstall": "electron-rebuild",
"electron-rebuild": "electron-rebuild"
},
The postinstallwill automatically run after doing a npm installso after the typical install finishes you'll see a console log message with electron-rebuildand it will automatically rebuild serialport, and any other native library you have, to the electronversion. This means that you shouldn't even have to think about running electron-rebuildgoing forward.
在postinstall做了之后会自动运行npm install,因此典型的后安装完成后,你会看到一个控制台日志消息electron-rebuild,它会自动重建serialport,以及任何其他本机库,您必须到electron版本。这意味着您甚至不必考虑electron-rebuild继续前进。
To manually re-run electron-rebuildjust run it with npm run electron-rebuild.
要手动重新运行,electron-rebuild只需使用npm run electron-rebuild.
Easy-peezie, lemon-squeezie!
Easy-peezie,柠檬挤压!
回答by d9ngle
Create file .npmrcwith content:
创建.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
Open another terminal and run npm install [yourpackage]
打开另一个终端并运行 npm install [yourpackage]
Keep in mind, some new packages would be installed with highest electron version (target), so save yourself some headache/backache and update your target =with the current version stated on npm or github page.
请记住,一些新软件包将使用最高的电子版本(目标)安装,因此您target =可以避免一些头痛/背痛,并使用 npm 或 github 页面上所述的当前版本更新您的软件包。

