Node.js 執行 Electron 出現”NODE_MODULE_VERSION 57. Please try re-compiling or re-installing” 解決方法

最近在使用 Electron 的時候它有時候會出現 NODE_MODULE_VERSION 57. Please try re-compiling or re-installing 的錯誤訊息,它的意思其實就是需要重開 build module

錯誤訊息

我目前遇到的有兩個 module 會出現分別是 iltorb 與 bcrypt 它的錯誤訊息如下:

App threw an error during load
Error: The module '/node_modules/iltorb/build/bindings/iltorb.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 93. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).

而 bcrypt 的錯誤如下

Error: The module '/node_modules/bcrypt/lib/binding/bcrypt_lib.node'
was compiled against a different Node.js version using
NODE_MODULE_VERSION 51. This version of Node.js requires
NODE_MODULE_VERSION 57. Please try re-compiling or re-installing
the module (for instance, using `npm rebuild` or `npm install`).

解決方法

方法一

其實它的解決方法很簡單通常我會直接安裝 Electron Rebuild 來解決

npm install --save-dev electron-rebuild

修改一下 package.js

"scripts": {
  "electron": "electron .",
  "rebuild": "electron-rebuild"
}

執行一下 rebuild

$ npm run rebuild
$ npm run electron

執行完後再開啟 Electron 應該就會正常了,如果還是不行可以試試看方法二

方法二

或是直接移除發生問題的 module 然後重新安裝,以上面的 module 為範例

$ rm -rf node_modules/iltorb
$ rm -rf node_modules/bcrypt
$ npm install

通常這樣就可以解決了

結論

這種需要使用 node-gyp 的 module 通常比較麻煩在不同版本的 Node.js 都會遇到這種問題,不過真的遇到就只能處理了。最後附上 NODE_MODULE_VERSION 版本的對照表,其實它是對應不同版本的 Node.js 的,之前找了好久找不到對照表 https://nodejs.org/zh-tw/download/releases/