解决报错:this[kHandle] = new _Hash(algorithm, xofLen);
前端项目编译报错:
node:internal/crypto/hash:68this[kHandle] = new _Hash(algorithm, xofLen);^Error: error:0308010C:digital envelope routines::unsupportedat new Hash (node:internal/crypto/hash:68:19)at Object.createHash (node:crypto:138:10)at module.exports (D:\code\work_code\qulian\zky-jsc\zky-screen-web\node_modules\webpack\lib\util\createHash.js:
135:53)
这个错误是由于 Node.js 的加密模块与当前项目使用的 OpenSSL 版本不兼容导致的。常见于 Node.js v17+ 版本,因为这些版本使用了 OpenSSL 3.0,而一些老项目可能依赖旧版的加密算法。
解决方案:
1. 临时解决方案(推荐先尝试)
在运行命令前设置环境变量:
export NODE_OPTIONS=--openssl-legacy-provider
Windows 系统使用:
set NODE_OPTIONS=--openssl-legacy-provider
2. 长期解决方案
修改项目的 package.json
,在启动脚本中加入环境变量:
"scripts": {"start": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts start","build": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts build"
}
3. 其他可选方案
-
降级 Node.js 到 v16(LTS 版本)
-
更新 webpack 和相关依赖到最新版本
-
检查项目是否有过时的加密相关依赖