首页 > 解决方案 > 如何在 ubuntu 18.04 中安装 bcrypt

问题描述

当我尝试通过命令“sudo npm install bcrypt --save”在 Ubuntu 18.04 中安装 bcrypt 时,出现错误:

node-pre-gyp WARN Using needle for node-pre-gyp https download 
node-pre-gyp WARN Pre-built binaries not installable for bcrypt@3.0.6 and node@10.16.3 (node-v64 ABI, glibc) (falling back to source compile with node-gyp) 
node-pre-gyp WARN Hit error EACCES: permission denied, mkdir '/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/lib' 
gyp ERR! configure error 
gyp ERR! stack Error: EACCES: permission denied, mkdir '/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/build'
gyp ERR! System Linux 5.0.0-25-generic
gyp ERR! command "/usr/bin/node" "/usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "configure" "--fallback-to-build" "--module=/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/lib/binding/bcrypt_lib.node" "--module_name=bcrypt_lib" "--module_path=/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/lib/binding" "--napi_version=4" "--node_abi_napi=napi" "--napi_build_version=0" "--node_napi_label=node-v64"
gyp ERR! cwd /home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt
gyp ERR! node -v v10.16.3
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 '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/lib/binding --napi_version=4 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/home/xuanhoang/NodeJS/Udemy/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:198:13)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:982:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:259:5)
node-pre-gyp ERR! System Linux 5.0.0-25-generic
node-pre-gyp ERR! command "/usr/bin/node" "/home/xuanhoang/NodeJS/Udemy/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt
node-pre-gyp ERR! node -v v10.16.3
node-pre-gyp ERR! node-pre-gyp -v v0.12.0
node-pre-gyp ERR! not ok 
Failed to execute '/usr/bin/node /usr/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js configure --fallback-to-build --module=/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/lib/binding/bcrypt_lib.node --module_name=bcrypt_lib --module_path=/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/lib/binding --napi_version=4 --node_abi_napi=napi --napi_build_version=0 --node_napi_label=node-v64' (1)
npm WARN udemy@1.0.0 No description
npm WARN udemy@1.0.0 No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! bcrypt@3.0.6 install: `node-pre-gyp install --fallback-to-build`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the bcrypt@3.0.6 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/xuanhoang/.npm/_logs/2019-09-01T08_14_32_072Z-debug.log

标签: npm-install

解决方案


以下行:

EACCES: permission denied, mkdir '/home/xuanhoang/NodeJS/Udemy/node_modules/bcrypt/build'

表示 sudo 用户没有写入该文件的权限。

这可以通过删除node_modules文件夹并再次安装依赖项来解决:

sudo rm -rf node_modules
npm i
npm install bcrypt --save

使用最新命令--save时默认启用该标志。npm


推荐阅读