Mac系统node:internal/crypto/hash:71 this[kHandle] = new _Hash(algorithm, xofLen);报错解决方法

this[kHandle] = new _Hash(algorithm, xofLen, algorithmId, getHashCache());

node:internal/crypto/hash:71  this[kHandle] = new _Hash(algorithm, xofLen);                  ^Error: error:0308010C:digital envelope routines::unsupported    at Object.createHash (node:crypto:133:10)    at NormalModule._initBuildHash (D:scodeworkspaceyyghyygh_site
ode_moduleswebpacklibNormalModule.js:417:16)         at handleParseError (D:scodeworkspaceyyghyygh_site
ode_moduleswebpacklibNormalModule.js:471:10)    at D:scodeworkspaceyyghyygh_site
ode_moduleswebpacklibNormalModule.js:503:5    at D:scodeworkspaceyyghyygh_site
ode_moduleswebpacklibNormalModule.js:358:12    at D:scodeworkspaceyyghyygh_site
ode_moduleswebpack
ode_modulesloader-runnerlibLoaderRunner.js:373:3    at iterateNormalLoaders (D:scodeworkspaceyyghyygh_site
ode_moduleswebpack
ode_modulesloader-runnerlibLoaderRunner.js:214:10)    at Array.<anonymous> (D:scodeworkspaceyyghyygh_site
ode_moduleswebpack
ode_modulesloader-runnerlibLoaderRunner.js:205:4) {  opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],  library: 'digital envelope routines',  reason: 'unsupported',  code: 'ERR_OSSL_EVP_UNSUPPORTED'}

问题原因:
今天在调试项目的时候,突然报错,原因大概率是node版本太高,我机器的Node版本是22,因为 node.js V17开始版本中发布的是OpenSSL3.0, 而OpenSSL3.0对允许算法和密钥大小增加了严格的限制,可能会对生态系统造成一些影响。故此以前的项目在使用 nodejs V17以上版本后会报错。而github项目很多都是之前版本的npm,所以运行时候会出现这个问题。

建议不要安装v18以上的版本,以下是使用Homebrew重新安装node的方法。

解决办法:

node -v //查看版本
which node //查看node安装位置
brew search node //查看你安装的所有Node版本

可以按照下面的步骤直接操作

brew uninstall node
brew uninstall node@你的版本 //例如node@22\node@18
brew uninstall --force libnghttp2  // 以上三步为了彻底删除node

brew install node@16 //安装16版本

配置环境变量

echo 'export PATH="/opt/homebrew/opt/node@16/bin:$PATH"' >> ~/.zshrc

生效命令

source ~/.zshrc 

查看是否安装成功

node -v
nmp -v
which node -- 查看node安装路径

你可能感兴趣的:(Mac,OS,问题解决,macos,哈希算法,算法)