使用npx create-react-app创建React项目失败,卸载清缓存仍报错
报错信息:
npm ERR! Unexpected end of JSON input while parsing near '..."optional":true}},"fu' npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\Zee\AppData\Roaming\npm-cache\_logs\2021-10-13T16_22_36_156Z-debug.log
报错原因
Unexpected end of JSON input while parsing 报错是npm拉取依赖包元数据时,文件下载不全导致JSON结构损坏无法解析,常见于网络不稳定、npm镜像访问出错、本地缓存残留损坏文件的场景。
修复步骤
- 切换为国内稳定npm镜像,解决网络访问导致的元数据下载不完整问题:
执行命令:
执行后可运行npm config set registry https://registry.npmmirror.com/npm config get registry验证是否切换成功。 - 手动删除本地npm缓存文件夹,彻底清除残留的损坏缓存文件:
根据报错提示的路径,直接删除C:\Users\Zee\AppData\Roaming\npm-cache整个文件夹即可。其他操作系统对应缓存路径:- macOS:
~/.npm - Linux:
~/.npm
- macOS:
- 升级npm到最新稳定版,规避低版本npm的已知解析缺陷:
执行命令:npm install -g npm@latest - 重新执行React项目创建命令,优先使用
npm init方式绕过npx临时缓存问题:
执行命令:npm init react-app 替换为你的项目名
如果上述操作后仍报错,可以尝试使用yarn创建项目:
yarn create react-app 替换为你的项目名
内容的提问来源于stack exchange,提问作者Zeamanuel Belay
相关产品推荐
相关产品推荐

