みかづきブログ その3

本ブログは更新を終了しました。通算140万ユーザーの方に観覧頂くことができました。長い間、ありがとうございました。

👆

引越し先はこちらです!

Electronとシリアル通信を両立する

Electronアプリでserialportをrequireして使おうとすると、

f:id:kimizuka:20160815235737j:plain

Uncaught Exception:
Error: Module version mismatch. Expected 49, got 47. at Error (native)

的なエラーが発生します。

そんな時は、アプリケーションのルートにて、

cd node_modules/serialport && ./node_modules/.bin/node-pre-gyp rebuild --target=1.3.2 --arch=x64 --dist-url=https://atom.io/download/atom-shell

と、実行するとエラーが無くなります。
(Macでは動作するのですが、書きだしたアプリをWindowsにインストールしてつかうとエラーになりました...)

package.jsonを、

{
  "name": "serialport",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "start": "electron .",
    "build": "electron-packager . app --platform=darwin --arch=x64 --overwrite",
    "postinstall": "cd node_modules/serialport && ./node_modules/.bin/node-pre-gyp rebuild --target=1.3.2 --arch=x64 --dist-url=https://atom.io/download/atom-shell"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "electron-packager": "^7.5.1",
    "electron": "^1.3.2",
    "serialport": "^4.0.1"
  }
}

という感じに編集してpostinstallに設定しておくと毎回実行しなくてすむのでおすすめです。