[Node.JS] bcrypt 安裝錯誤解決方案

某些服務會使用 bcrypt 來進行 hash secrets,像是:Aqua、Drywall、PartJS、MakeeAPI 等。但在 Windows 下安裝 bcrypt 時常會遇到與多問題及錯誤 (如下所示),這時若只是單純想把服務運作起來,我們可以使用 bcryptjs 來代替。

$ npm install bcrypt
-
> [email protected] install C:\Users\POS\node_modules\bcrypt
> node-gyp rebuild


C:\Users\POS\node_modules\bcrypt>if not defined npm_config_node_gyp (node "C:\Users\larry\AppData\Roaming\nvm\v4.3.0\node_modules\  
npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\bin\node-gyp.js" rebuild )  else (node  rebuild )  
在這個方案中一次建置一個專案。若要啟用平行組建,請加入 "/m" 參數。
  blowfish.cc
  bcrypt.cc
  bcrypt_node.cc
C:\Users\POS\node_modules\bcrypt\node_modules\nan\nan.h(72): fatal error C1060: 編譯器堆積空間不足 (..\src\bcrypt_node.cc) [C:\Users\POS\node_modules\bcrypt\build\bcrypt_lib.vcxproj]  
..\src\bcrypt.cc(232): warning C4267: '=' : 將 'size_t' 轉換為 'unsigned char',資料可能遺失 [C:\Users\POS\node_modules\bcrypt\build\
bcrypt_lib  
.vcxproj]
gyp ERR! build error  
gyp ERR! stack Error: `C:\Windows\Microsoft.NET\Framework\v4.0.30319\msbuild.exe` failed with exit code: 1  
gyp ERR! stack     at ChildProcess.onExit (C:\Users\larry\AppData\Roaming\nvm\v4.3.0\node_modules\npm\node_modules\node-gyp\lib\build.js:270:23)  
gyp ERR! stack     at emitTwo (events.js:87:13)  
gyp ERR! stack     at ChildProcess.emit (events.js:172:7)  
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:200:12)  
gyp ERR! System Windows_NT 6.1.7601  
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\larry\\AppData\\Roaming\\nvm\\v4.3.0\\node_modules\\npm\\node_modules\\node-gyp\  
\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\POS\node_modules\bcrypt  
gyp ERR! node -v v4.3.0  
gyp ERR! node-gyp -v v3.0.3  
gyp ERR! not ok  
npm ERR! Windows_NT 6.1.7601  
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "bcrypt"  
npm ERR! node v4.3.0  
npm ERR! npm  v2.14.12  
npm ERR! code ELIFECYCLE

npm ERR! [email protected] install: `node-gyp rebuild`  
npm ERR! Exit status 1  
npm ERR!  
npm ERR! Failed at the [email protected] install script 'node-gyp rebuild'.  
npm ERR! This is most likely a problem with the bcrypt package,  
npm ERR! not with npm itself.  
npm ERR! Tell the author that this fails on your system:  
npm ERR!     node-gyp rebuild  
npm ERR! You can get their info via:  
npm ERR!     npm owner ls bcrypt  
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:  
npm ERR!     C:\Users\POS\npm-debug.log  

解決方案

解決的方案很簡單,直接將套件 bcrypt 改成 bcryptjs 即可。

請先進行安裝:

$ npm i bcryptjs

接著開始修改程式碼。

原本:

var bcrypt = require('bcrypt');  

修改後:

var bcrypt = require('bcryptjs');  

版本資訊:

  • NodeJS:v4.3.0
  • bcryptjs: ~2.0.2
ALL RIGHTS RESERVED. COPYRIGHT © 2016. Designed and Coded by Makee.io