创建React项目时报core-js@2.6.12 postinstall执行失败错误如何解决
我此前一直通过Facebook的GitHub仓库创建React项目,每次都能正常运行,但本次操作失败,出现如下报错,我已经尝试清理缓存,仍找不到问题原因。
> core-js@2.6.12 postinstall C:\Users\user\Desktop\cryptohello\node_modules\babel-runtime\node_modules\core-js > node -e "try{require('./postinstall')}catch(e){}" The system cannot find the path specified. npm ERR! code ELIFECYCLE npm ERR! errno 3 npm ERR! core-js@2.6.12 postinstall: `node -e "try{require('./postinstall')}catch(e){}"` npm ERR! Exit status 3 npm ERR! npm ERR! Failed at the core-js@2.6.12 postinstall script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\user\AppData\Roaming\npm-cache\_logs\2021-08-15T16_48_09_293Z-debug.log Aborting installation. npm install --save --save-exact --loglevel error react react-dom react-scripts cra-template has failed. Deleting generated file... node_modules Deleting generated file... package.json Deleting cryptohello/ from C:\Users\user\Desktop Done.
报错原因
该报错为Windows环境下core-js@2.6.12包的postinstall安装钩子执行失败,通常由Node/npm版本兼容性、路径解析异常导致。
解决方案
- 检查Node版本适配性
优先使用Node.js LTS稳定版本(推荐14.x/16.x版本,不要使用过新的非稳定版或低于12.x的老旧版本),执行node -v可查看当前安装版本,版本不符先卸载重装对应LTS版Node后重试创建项目。 - 跳过安装钩子执行
创建项目时添加忽略脚本参数,直接执行命令:npx create-react-app cryptohello --ignore-scripts
跳过core-js的postinstall校验,不会影响项目后续正常运行。 - 清理残留后重试
先删除创建失败生成的不完整项目文件夹,执行npm cache clean --force全局清理npm缓存,再重新执行项目创建命令。 - 切换包管理器
如果npm始终报错,可切换为yarn创建项目,先全局安装yarn:npm install -g yarn,再执行yarn create react-app cryptohello即可。
内容的提问来源于stack exchange,提问作者Chen
相关产品推荐
相关产品推荐

