安装react-scroll遇版本问题,执行npm命令后出现ERR_OSSL_EVP_UNSUPPORTED错误求助
解决ERR_OSSL_EVP_UNSUPPORTED错误(Node.js 18+ React项目)
错误原因
Node.js 18及以上版本默认启用了严格的SSL加密策略,禁用了旧的哈希算法。你的项目依赖的Webpack版本(react-scripts内置)仍在使用这些被禁用的算法,导致加密流程报错,最终项目无法启动。
解决方案
方案1:临时添加环境变量快速修复
修改项目根目录下的package.json,在启动/构建脚本前添加NODE_OPTIONS=--openssl-legacy-provider:
"scripts": { "start": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts start", "build": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" }
修改后运行npm start即可启动项目。
方案2:升级react-scripts到兼容版本(根治)
- 卸载旧版react-scripts:
npm uninstall react-scripts
- 安装最新稳定版:
npm install react-scripts@latest
- 删除
node_modules和package-lock.json,重新安装依赖:
rm -rf node_modules package-lock.json npm install
- 启动项目:
npm start
方案3:回退Node.js版本到16.x
如果不想升级依赖,可通过nvm切换到Node.js 16版本(该版本默认支持旧哈希算法):
- 安装Node.js 16:
nvm install 16
- 切换到16版本:
nvm use 16
- 重新安装依赖并启动:
npm install npm start
额外提示
之前执行的npm audit fix --force可能强制修改了多个依赖版本,引发连锁兼容问题。建议在采用方案2或3后,彻底清理依赖缓存并重新安装,避免残留异常。
内容的提问来源于stack exchange,提问作者Web_dev_Learner
相关产品推荐
相关产品推荐

