接手React项目执行npm run deploy时提示'react-scripts'未被识别,如何解决?
问题描述
刚接手同事的React项目,本地拉取代码后直接执行部署命令npm run deploy,终端连续抛出报错,完整输出如下:
C:\Users\Danielle\Documents\GitHub\aussieElectro> npm run deploy scrollyteller-react@0.1.0 predeploy C:\Users\Danielle\Documents\GitHub\aussieElectro > npm run build scrollyteller-react@0.1.0 build C:\Users\Danielle\Documents\GitHub\aussieElectro react-scripts build 'react-scripts' is not recognised as an internal or external comm, and operable program or batch file. npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! scrollyteller-react@0.1.0 build: 'react-scripts build' npm ERR! Exist status 1 npm ERR! @0.1.0 predeploy script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm WARN Local package.json exists, but node_modules missing, did you mean to install?
修复步骤
报错根因非常明确:从Git拉取的本地代码里,缺失项目运行需要的依赖文件——也就是警告最后一行提到的node_modules文件夹不存在。react-scripts是React项目的标准构建工具,本身就存放在依赖文件目录中,本地没有安装依赖,系统自然识别不到这个可执行命令,直接触发运行错误。
按顺序操作即可解决:
- 打开终端,将路径切换到项目根目录(即存放
package.json文件的文件夹层级) - 执行命令
npm install,等待进程跑完- 这个命令会自动读取
package.json中记录的所有依赖版本,把项目需要的工具包全部下载到本地的node_modules目录下
- 这个命令会自动读取
- 依赖安装完成后,重新执行
npm run deploy,即可正常走完构建、部署流程 - 如果安装依赖过程中卡住或抛出错误,先执行
npm cache clean --force清空本地npm缓存,再重新运行一遍npm install即可
小提醒:所有上传到Git远程仓库的前端项目,默认都会把
node_modules文件夹加入忽略规则,不会把几百兆的依赖包上传到远程地址。所有人拉取代码后,必须先执行依赖安装步骤才能运行项目脚本,不要刚拉完代码就直接执行部署命令。
内容的提问来源于stack exchange,提问作者Danimjs
相关产品推荐
相关产品推荐

