![](/img/trans.png)
[英]Electron require module error for externally installed node modules in renderer process
[英]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.