You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行npx react-native init AppName报ENOTEMPTY错误如何解决

问题描述

执行npx react-native init AppName命令初始化React Native应用时持续报错,命令执行交互流程及完整报错信息如下:

Need to install the following packages:
  react-native@0.69.0
Ok to proceed? (y) y
npm ERR! code ENOTEMPTY
npm ERR! syscall rename
npm ERR! path /Users/risman/.npm/_npx/7930a8670f922cdb/node_modules/react-native
npm ERR! dest /Users/risman/.npm/_npx/7930a8670f922cdb/node_modules/.react-native-tr3sR9QU
npm ERR! errno -66
npm ERR! ENOTEMPTY: directory not empty, rename '/Users/risman/.npm/_npx/7930a8670f922cdb/node_modules/react-native' -> '/Users/risman/.npm/_npx/7930a8670f922cdb/node_modules/.react-native-tr3sR9QU'

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/risman/.npm/_logs/2022-06-27T14_07_36_253Z-debug-0.log
报错原因

该错误属于npm/npx的缓存冲突问题:之前执行npx拉取react-native包时出现过中断、异常退出的情况,导致缓存目录下残留了未清理完全的旧文件,后续执行命令时npm尝试重命名目录完成版本更新,因目标目录存在残留文件非空,触发ENOTEMPTY系统错误终止流程。

解决方法

按优先级依次尝试以下操作即可修复:

  • 精准删除报错信息中提到的冲突缓存目录,执行命令:
    rm -rf /Users/risman/.npm/_npx/7930a8670f922cdb/
    执行完成后直接重新运行初始化命令,多数场景下这一步就能解决问题。
  • 如果上述操作后仍报错,直接强制清空npm全量缓存:
    npm cache clean --force
  • 清空缓存后关闭当前终端,重新打开新终端窗口执行初始化命令,建议初始化时明确指定要安装的react-native版本,减少临时拉取版本触发缓存异常的概率,示例命令:
    npx react-native@0.69.0 init AppName

内容的提问来源于stack exchange,提问作者Charisman Apriandi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.28 10:36:22