如何修改React项目依赖?旧React项目重装报错求助
旧React项目安装/启动报错解决方法
从报错截图来看,你遇到的是Error: error:0308010C:digital envelope routines::unsupported,这是Node.js 16及以上版本默认启用了新的OpenSSL算法,旧版react-scripts或webpack依赖的加密逻辑不兼容导致的,和单纯升级React版本无关,以下是几种解决方式:
临时启动项目
在终端执行启动命令前添加环境变量:
- Windows(命令提示符):
set NODE_OPTIONS=--openssl-legacy-provider && npm start - Windows(PowerShell):
$env:NODE_OPTIONS="--openssl-legacy-provider"; npm start - Mac/Linux:
NODE_OPTIONS=--openssl-legacy-provider npm start
永久配置启动脚本
修改项目根目录下的package.json,替换原有启动、构建、测试脚本:
"scripts": { "start": "NODE_OPTIONS=--openssl-legacy-provider react-scripts start", "build": "NODE_OPTIONS=--openssl-legacy-provider react-scripts build", "test": "NODE_OPTIONS=--openssl-legacy-provider react-scripts test" }
Windows用户若使用命令提示符,需调整为:
"scripts": { "start": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts start", "build": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts build", "test": "set NODE_OPTIONS=--openssl-legacy-provider && react-scripts test" }
升级核心依赖(彻底解决)
如果临时配置不想长期用,可以升级react-scripts到最新版本,它已经兼容Node.js 16+:
npm install react-scripts@latest
升级后若出现依赖冲突,可删除node_modules文件夹和package-lock.json文件,再重新执行npm install。
内容的提问来源于stack exchange,提问作者apanay22
相关产品推荐
相关产品推荐

