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

执行react-native init初始化项目报cli.init is not a function错误

执行npx react-native init Sarfaraz初始化项目报错TypeError: cli.init is not a function

错误场景

本地目标文件夹下执行React Native项目初始化命令:
npx react-native init Sarfaraz
程序运行异常退出,输出错误堆栈:

C:\Users\HP\AppData\Roaming\npm\node_modules\react-native-cli\index.js:302
cli.init(root, projectName);
^

TypeError: cli.init is not a function
at run (C:\Users\HP\AppData\Roaming\npm\node_modules\react-native-cli\index.js:302:7)
at createProject (C:\Users\HP\AppData\Roaming\npm\node_modules\react-native-cli\index.js:249:3)
at init (C:\Users\HP\AppData\Roaming\npm\node_modules\react-native-cli\index.js:200:5)
at Object.<anonymous> (C:\Users\HP\AppData\Roaming\npm\node_modules\react-native-cli\index.js:153:7)
at Module._compile (node:internal/modules/cjs/loader:1105:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47

报错原因

本地全局环境残留了官方已废弃的旧版react-native-cli包。React Native 0.69及以上版本已经移除了对独立全局react-native-cli的支持,初始化逻辑统一整合到社区维护的@react-native-community/cli中,旧全局包的代码逻辑和新版初始化流程不兼容,尝试调用不存在的init方法就会触发该类型错误。

解决步骤

  • 卸载全局安装的旧版react-native-cli,根据自身使用的包管理器执行对应命令:
    npm用户执行:
    npm uninstall -g react-native-cli
    yarn用户执行:
    yarn global remove react-native-cli
  • 执行命令清理npm缓存,避免旧包残留影响后续拉取:
    npm cache clean --force
  • 重新执行初始化命令,显式指定拉取最新版React Native初始化脚本,避免缓存调用旧逻辑:
    npx react-native@latest init Sarfaraz

内容的提问来源于stack exchange,提问作者Sarfaraz Raza Qadri

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.29 03:27:20