创建React应用时反复出现相同错误,package.json未生成scripts字段

临时解决方法
直接手动在你现有的package.json中补充scripts字段即可正常使用,修改后的完整文件内容如下:
{ "name": "my-app", "version": "0.1.0", "private": true, "dependencies": { "cra-template": "1.1.2", "postinstall-js": "^0.7.1", "react": "17.0.2", "react-dom": "17.0.2", "react-scripts": "4.0.3" }, "scripts": { "start": "react-scripts start", "build": "react-scripts build", "test": "react-scripts test", "eject": "react-scripts eject" } }
修改保存后执行npm start就能正常启动项目。
彻底解决该问题的方案
- 先卸载本地全局安装的旧版create-react-app:
npm uninstall -g create-react-app,后续创建React项目统一使用npx create-react-app 项目名的命令,直接拉取最新版本的模板,避免本地版本兼容问题。 - 要是卸载后重新创建仍有问题,执行
npm cache clean --force清理npm损坏的缓存后再重试。 - 网络不佳的情况下,可以指定国内镜像源创建项目:
npx create-react-app 项目名 --registry https://registry.npmmirror.com。
内容的提问来源于stack exchange,提问作者Rachit
相关产品推荐
相关产品推荐

