You need to enable JavaScript to run this app. React Native 0.71.3初始化时cli.init不是函数的报错咨询
优惠活动
大模型
产品
解决方案
定价
更多

React Native 0.71.3初始化时cli.init不是函数的报错咨询

React Native 0.71.3 初始化项目报错:TypeError: cli.init is not a function

报错信息

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

^

TypeError: cli.init is not a function
    在 run 函数中 (C:\Users\katyal\AppData\Roaming\npm\node_modules\react-native-cli\index.js:302:7)
    在 createProject 函数中 (C:\Users\katyal\AppData\Roaming\npm\node_modules\react-native-cli\index.js:249:3)
    在 init 函数中 (C:\Users\katyal\AppData\Roaming\npm\node_modules\react-native-cli\index.js:200:5)
    在 Object.<anonymous> 中 (C:\Users\katyal\AppData\Roaming\npm\node_modules\react-native-cli\index.js:153:7)
    在 Module._compile (node:internal/modules/cjs/loader:1275:14)
    在 Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    在 Module.load (node:internal/modules/cjs/loader:1133:32)
    在 Module._load (node:internal/modules/cjs/loader:972:12)
    在 Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12)
    在 node:internal/main/run_main_module:23:47

解决方案

该错误源于全局安装的react-native-cli与React Native 0.60+版本不兼容,新版本已弃用全局CLI,改用项目内置的@react-native-community/cli。

1. 卸载全局react-native-cli

打开终端/命令提示符执行:

npm uninstall -g react-native-cli
# 若使用yarn则执行:
yarn global remove react-native-cli

2. 用官方推荐命令初始化项目

直接通过npx调用对应版本的React Native CLI初始化,无需全局依赖:

npx react-native@0.71.3 init YourProjectName

若需TypeScript模板:

npx react-native@0.71.3 init YourProjectName --template react-native-template-typescript

3. 验证项目运行

进入项目目录后,分别执行对应平台命令:

cd YourProjectName
# 运行Android
npx react-native run-android
# 运行iOS(仅Mac环境)
npx react-native run-ios

额外排查项

  • 若仍有缓存问题,清理npm缓存:
npm cache clean --force
  • 确认Node.js版本符合要求(React Native 0.71.3推荐16.x或18.x),可通过node -v查看版本,不符则调整Node.js版本。

内容的提问来源于stack exchange,提问作者deepanshu katyal

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 02:17:26