みかづきブログ・カスタム

基本的にはちょちょいのほいです。

Error: The module 'hogehoge.node' was compiled against a different Node.js version using NODE_MODULE_VERSION XX. This version of Node.js requires NODE_MODULE_VERSION YY. Please try re-compiling or re-installing の解決を目指してelectron-rebuildを実行する 🔨

electronmdns を同時に使おうと思ったところ、

App threw an error during load
Error: The module '/Users/nanashi/develop/node_modules/mdns/build/Release/dns_sd_bindings.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 83. This version of Node.js requires
NODE_MODULE_VERSION 80. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).
at process.func [as dlopen] (electron/js2c/asar.js:140:31)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:1034:18)
at Object.func [as .node] (electron/js2c/asar.js:140:31)
at Module.load (internal/modules/cjs/loader.js:815:32)
at Module._load (internal/modules/cjs/loader.js:727:14)
at Function.Module._load (electron/js2c/asar.js:769:28)
at Module.require (internal/modules/cjs/loader.js:852:19)
at require (internal/modules/cjs/helpers.js:74:18)
at Object. (/Users/nanashi/develop/node_modules/mdns/lib/dns_sd.js:24:20)
at Module._compile (internal/modules/cjs/loader.js:967:30)

というエラーが発生。

エラーを読むに、node.jsのバージョンをNODE_MODULE_VERSIONが80のものに変更すれば解決すると思い、リリース一覧に目を通すも、

f:id:kimizuka:20200808085019p:plain

80がない。79の次が83に飛んでいる。

どうしたものかと思ったが、

Please try re-compiling or re-installingt he module (for instance, using `npm rebuild` or `npm install`).

という文があるため、もう一回コンパイルかインストールすることを試みることにした。

とりあえず、

npm rebuild

を実行してみるものの、結果は変わらず。困った。

いろいろ調べてみると、 electron-rebuild たるパッケージを発見。

早速、

github.com

yarn add -D electron-rebuild

で、インストールし、

./node_modules/.bin/electron-rebuild

で実行してみたところ。無事にエラーが解消。
内部でなにが行われているのかは、まったくわかってないが、とりあえず動くようになったので今回は良しとする。

ディスクリプションを読むに、

This executable rebuilds native Node.js modules against the version of Node.js that your Electron project is using. This allows you to use native Node.js modules in Electron apps without your system version of Node.js matching exactly (which is often not the case, and sometimes not even possible).

この実行可能ファイルは、Electronプロジェクトが使用しているバージョンのNode.jsに対してネイティブNode.jsモジュールを再構築します。これにより、Node.jsのシステムバージョンが正確に一致しなくても、ElectronアプリでネイティブNode.jsモジュールを使用できます(多くの場合そうではなく、場合によっては不可能です)。

と書いてあるので、node_modulesの中身をバージョンに合わせていい感じにrebuildしてくれたのだろう。ありがたい限り。