启动项目时报错,如下:
node:internal/crypto/hash:71
this[kHandle] = new _Hash(algorithm, xofLen);
^
Error: error:0308010C:digital envelope routines::unsupported
at new Hash (node:internal/crypto/hash:71:19)
at Object.createHash (node:crypto:140:10)
{
opensslErrorStack: [ 'error:03000086:digital envelope routines::initialization error' ],
library: 'digital envelope routines',
reason: 'unsupported',
code: 'ERR_OSSL_EVP_UNSUPPORTED'
}
出现这个错误的原因是,node版本太高,跟openssl不匹配,node官方也说明: Node.js v17.x, v18.x, and v19.x use OpenSSL v3.
我的配置:
node版本:node -v
=> v19.8.1
openssl版本:openssl version
=> LibreSSL 2.8.3
搜索了一番,在stackoverflow中搜到了解决方案。
方法一:
最简单
降低node版本,node<17以下即可,我这儿使用的nvm降低node到 v16.20.0,就可以正常启动项目了。
方法二:
设置node环境变量的参数: export NODE_OPTIONS=--openssl-legacy-provider
windows端cmd:set NODE_OPTIONS=--openssl-legacy-provider
windows端PowerShell: $env:NODE_OPTIONS = "--openssl-legacy-provider"
担心影响其他使用到node的程序,没有采用。
方法三:
跟上一种类似,只不过加入到启动项目的命令中
React:
"scripts": {
"start": "export SET NODE_OPTIONS=--openssl-legacy-provider && react-scripts start",
"build": "export SET NODE_OPTIONS=--openssl-legacy-provider && react-scripts build"
}
or
"scripts": {
"start": "react-scripts --openssl-legacy-provider start",
"build": "react-scripts --openssl-legacy-provider build",
}
Vue.js:
"scripts": {
"serve": "export NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service serve",
"build": "export NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service build",
"lint": "export NODE_OPTIONS=--openssl-legacy-provider && vue-cli-service lint"
}
or
"scripts": {
"serve": "vue-cli-service --openssl-legacy-provider serve",
"build": "vue-cli-service --openssl-legacy-provider build",
"lint": "vue-cli-service --openssl-legacy-provider lint"
}
Angular:
"scripts": {
"start": "set NODE_OPTIONS=--openssl-legacy-provider && gulp buildDev && ng serve ",
"publish": "set NODE_OPTIONS=--openssl-legacy-provider && gulp build && ng build --prod",
}
方法四:
修改webpack版本,跟webpack中使用了md4哈希算法有关,在v5.61.0中使用了wasm实现的md4算法
- https://github.***/webpack/webpack/releases/tag/v5.61.0
- https://github.***/webpack/webpack/issues/14532
or
如果webpack版本大于5.54,可以尝试一下修改experiments.futureDefaults: true
和output.hashFunction: 'xxhash64'
,我没有尝试 - 配置hashFunction:https://webpack.js.org/configuration/output/#outputhashfunction
但是更改webpack版本或者是修改output配置,也不一定就可以解决全部的问题,如果有别的node_modules包中,也使用了node的crypto.hash,那么还是会出问题,个人感觉还是降低node版本最划算。
参考链接:
- https://www.jianshu.***/p/edbad6875b7e#fn2
- https://stackoverflow.***/questions/69665222/node-js-17-0-1-gatsby-error-digital-envelope-routinesunsupported-err-os?answertab=scoredesc#tab-top
- https://github.***/webpack/webpack/releases/tag/v5.61.0
- https://github.***/webpack/webpack/issues/14532