You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows系统下使用npm安装bcrypt失败的问题求助

Windows下npm安装bcrypt失败的解决办法

问题回顾

Windows 10(版本22621)系统中,使用Node.js v20.5.0安装bcrypt时失败,已尝试以下操作但无效:

  • 替换为bcryptjs:npm install --save bcryptjs && npm uninstall --save bcrypt
  • 全局安装node-gyp:npm install -g node-gyp
  • 安装windows-build-tools:npm install --g --production windows-build-tools

执行npm install bcrypt后出现node-gyp和node-pre-gyp构建错误,核心报错为node-gyp配置执行失败,具体日志如下:

npm ERR! gyp ERR! node -v v20.5.0
npm ERR! gyp ERR! node-gyp -v v9.4.0
npm ERR! gyp ERR! not ok
npm ERR! node-pre-gyp ERR! build error
npm ERR! node-pre-gyp ERR! stack Error: Failed to execute 'D:\Programs\NodeJs\node.exe C:\Users\User\AppData\Roaming\npm\node_modules\npm\node_modules\node-gyp\bin\node-gyp.js configure --fallback-to-build --module=F:\Web dev projects\FusionReact\fusion_backend\node_modules\bcrypt\lib\binding\napi-v3\bcrypt_lib.node --module_name=bcrypt_lib --module_path=F:\Web dev projects\FusionReact\fusion_backend\node_modules\bcrypt\lib\binding\napi-v3 --napi_version=9 --node_abi_napi=napi --napi_build_version=3 --node_napi_label=napi-v3' (1)
npm ERR! node-pre-gyp ERR! stack at ChildProcess. (F:\Web dev projects\FusionReact\fusion_backend\node_modules@mapbox\node-pre-gyp\lib\util\compile.js:89:23)
npm ERR! node-pre-gyp ERR! stack at ChildProcess.emit (node:events:514:28)
npm ERR! node-pre-gyp ERR! stack at maybeClose (node:internal/child_process:1105:16)
npm ERR! node-pre-gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:305:5)
npm ERR! node-pre-gyp ERR! System Windows_NT 10.0.22621
npm ERR! node-pre-gyp ERR! command "D:\Programs\NodeJs\node.exe" "F:\Web dev projects\FusionReact\fusion_backend\node_modules\@mapbox\node-pre-gyp\bin\node-pre-gyp" "install" "--fallback-to-build"
npm ERR! node-pre-gyp ERR! cwd F:\Web dev projects\FusionReact\fusion_backend\node_modules\bcrypt
npm ERR! node-pre-gyp ERR! node -v v20.5.0
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.11
npm ERR! node-pre-gyp ERR! not ok

针对性解决方案

1. 匹配Node.js与bcrypt版本

bcrypt对Node.js版本有严格兼容要求,Node.js v20需安装bcrypt ^5.1.0及以上版本,执行指定版本安装:

npm install bcrypt@5.1.0

2. 修复node-gyp环境配置

  • 确保已安装Python 3.10(node-gyp v9.x要求Python版本为3.6-3.10),并将Python添加到系统环境变量PATH中。
  • 以管理员身份打开PowerShell,执行:
    npm config set python python3.10
    npm config set msvs_version 2022
    
    注:msvs_version需匹配你安装的Visual Studio版本,若装的是2019则设为2019

3. 清除缓存后重新安装

npm cache clean --force
rm -rf node_modules package-lock.json
npm install bcrypt

4. 强制使用预编译二进制包

设置环境变量跳过本地构建,直接下载对应平台的预编译包:

set BCYPT_BINARY=https://github.com/kelektiv/node.bcrypt.js/releases/download/v5.1.0/bcrypt_lib-v5.1.0-napi-v3-win32-x64.tar.gz
npm install bcrypt

注意替换链接中的版本号为你要安装的bcrypt版本

5. 替代方案:使用bcryptjs

如果以上方法均无效,bcryptjs是纯JavaScript实现,无需编译,可直接稳定使用:

npm install --save bcryptjs

代码中只需将require('bcrypt')替换为require('bcryptjs'),API完全兼容。


内容的提问来源于stack exchange,提问作者Joy0x1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 20:08:21