Open rami1973 opened 6 years ago
That's actually an issue with Bcrypt itself.
Bcrypt depends on node-gyp for native code which installs using prebuilt binaries. If it doesn't find a pre-built binary for the version you are using (like it doesn't have one for your versions bcrypt@1.0.3 and node@10.12.0) then it tries to build it from the source. This requires you to have Python 2, a C++ compiler, and a make system installed. And it isn't finding it on your system so it is failing.
You can either install those and have your computer build the binary you need or you can do a workaround like I did.
Remove bcrypt and bcrypt-promise, and install bcrypt-nodejs. Then replace the requires for bcrypt with require('bcrypt-nodejs').
I couldn't get bcrypt-nodejs-as-promised to replace bcrypt-promise so I had to write my own function for the hash promise in user.model.js:
async function bcryptHash(bPass, bSalt) { return await new Promise((resolve, reject) => { bcrypt.hash(bPass, bSalt, null, function (err, hash) { if (err) reject(err) resolve(hash) }); }) }
And then in the call to the bcrypt_p.hash I just replace it with my own function :
[err, hash] = await to(bcryptHash(user.password, salt));
This got it running for me without installing the Python 2, a C++ compiler, and a make system.
or you could do npm update instead?
Use bcryptjs and modify bcrypt-promise require bcrypt into bcryptjs
如果bcrypt报错,大部分原因是因为bcrypt的版本与node版本出现了不兼容导致的。解决这个问题比较简单。首先删掉node_modules和package-lock.json。然后到npm网站查询bcrypt与node对照表。node版本在10,11版本,bcrypt使用>=3;node版本在12,bcrypt使用>= 3.0.6;因为我的node版本是14.18,所以我直接把bcrypt版本改成了5.0.1,然后npm install,成功安装,没报错。
when running: npm install: rmktest@nodejs-tests-vm-vm:~/projects/node_rest_api_mysql$ npm install
node-pre-gyp ERR! Tried to download(404): https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.3/bcrypt_lib-v1.0.3-node-v64-linux-x64.tar.gz node-pre-gyp ERR! Pre-built binaries not found for bcrypt@1.0.3 and node@10.12.0 (node-v64 ABI) (falling back to source compile with node-gyp) node-pre-gyp ERR! Tried to download(undefined): https://github.com/kelektiv/node.bcrypt.js/releases/download/v1.0.3/bcrypt_lib-v1.0.3-node-v64-linux-x64.tar.gz node-pre-gyp ERR! Pre-built binaries not found for bcrypt@1.0.3 and node@10.12.0 (node-v64 ABI) (falling back to source compile with node-gyp) make: Entering directory '/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/build' make: Entering directory '/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/build' CXX(target) Release/obj.target/bcrypt_lib/src/blowfish.o CXX(target) Release/obj.target/bcrypt_lib/src/blowfish.o rm: cannot remove './Release/.deps/Release/obj.target/bcrypt_lib/src/blowfish.o.d.raw': No such file or directory bcrypt_lib.target.mk:103: recipe for target 'Release/obj.target/bcrypt_lib/src/blowfish.o' failed make: [Release/obj.target/bcrypt_lib/src/blowfish.o] Error 1 make: Leaving directory '/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/build' gyp ERR! build error gyp ERR! stack Error: (/home/rmktest/projects/node_rest_api_mysql/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack at ChildProcess.emit (events.js:182:13)
node-pre-gyp ERR! stack at maybeClose (internal/child_process.js:962:16)
node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:251:5)
node-pre-gyp ERR! System Linux 4.9.0-8-amd64
node-pre-gyp ERR! command "/opt/bitnami/nodejs/bin/.node.bin" "/home/rmktest/projects/node_rest_api_mysql/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt
node-pre-gyp ERR! node -v v10.12.0
node-pre-gyp ERR! node-pre-gyp -v v0.6.36
node-pre-gyp ERR! not ok
Failed to execute '/opt/bitnami/nodejs/bin/.node.bin /opt/bitnami/nodejs/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/lib/binding' (1)
../src/bcrypt.cc:322:1: fatal error: opening dependency file ./Release/.deps/Release/obj.target/bcrypt_lib/src/bcrypt.o.d.raw: No such file or directory
}
^
compilation terminated.
bcrypt_lib.target.mk:103: recipe for target 'Release/obj.target/bcrypt_lib/src/bcrypt.o' failed
make: [Release/obj.target/bcrypt_lib/src/bcrypt.o] Error 1
make: Leaving directory '/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/build'
gyp ERR! build error
gyp ERR! stack Error:
make
failed with exit code: 2 gyp ERR! stack at ChildProcess.onExit (/opt/bitnami/nodejs/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23) gyp ERR! stack at ChildProcess.emit (events.js:182:13) gyp CXX(target) Release/obj.target/bcrypt_lib/src/bcrypt.o ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12) gyp ERR! System Linux 4.9.0-8-amd64 gyp ERR! command "/opt/bitnami/nodejs/bin/.node.bin" "/opt/bitnami/nodejs/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/lib/binding/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/lib/binding" gyp ERR! cwd /home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt gyp ERR! node -v v10.12.0 gyp ERR! node-gyp -v v3.8.0 gyp ERR! not ok node-pre-gyp ERR! build error node-pre-gyp ERR! stack Error: Failed to execute '/opt/bitnami/nodejs/bin/.node.bin /opt/bitnami/nodejs/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/lib/binding' (1) node-pre-gyp ERR! stack at ChildProcess.make
failed with exit code: 2 gyp ERR! stack at ChildProcess.onExit (/opt/bitnami/nodejs/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:262:23) gyp ERR! stack at ChildProcess.emit (events.js:182:13) gyp ERR! stack at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12) gyp ERR! System Linux 4.9.0-8-amd64 gyp ERR! command "/opt/bitnami/nodejs/bin/.node.bin" "/opt/bitnami/nodejs/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/lib/binding/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt/lib/binding" gyp ERR! cwd /home/rmktest/projects/node_rest_api_mysql/node_modules/bcrypt gyp ERR! node -v v10.12.0 gyp ERR! node-gyp -v v3.8.0 gyp ERR! not ok npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! bcrypt@1.0.3 install:node-pre-gyp install --fallback-to-build
npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the bcrypt@1.0.3 install script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.npm ERR! A complete log of this run can be found in: npm ERR! /home/rmktest/.npm/_logs/2018-10-30T07_44_00_873Z-debug.log rmktest@nodejs-tests-vm-vm:~/projects/node_rest_api_mysql$