React Native 0.70执行npm run android报错:找不到指定模块
npm run android 报错:MODULE_NOT_FOUND 找不到 cli/build/bin.js 执行npm run android时出现以下错误:
FAILURE: Build failed with an exception.
Where:
Script 'C:\Users\mtepe\OneDrive\Masa�st�\react-native\firstProject\node_modules@react-native-community\cli-platform-android\native_modules.gradle' line: 413
What went wrong:
A problem occurred evaluating script.
node:internal/modules/cjs/loader:1080 throw err; ^Error: Cannot find module 'C:\Users\mtepe\OneDrive\Masaüstü\react-native\firstProject\node_modules@react-native-community\cli\build\bin.js' at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15) at Module._load (node:internal/modules/cjs/loader:922:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) at node:internal/main/run_main_module:23:47 { code: 'MODULE_NOT_FOUND', requireStack: []}Node.js v18.17.0
Try:
Run with --stacktrace option to get the stack trace.
Run with --info or --debug option to get more log output.
Run with --scan to get full insights.
Get more help at https://help.gradle.org
BU�LD FAILED in 2s
error Failed to install the app. Make sure you have the Android development environment set up: https://reactnative.dev/docs/environment-setup.
Error: Command failed: gradlew.bat app:installDebug -PreactNativeDevServerPort=8081:ReactNative:Unexpected empty result of running '[node, C:\Users\mtepe\OneDrive\Masaüstü\react-native\firstProject\node_modules@react-native-community\cli\build\bin.js, config]' command.
:ReactNative:Running '[node, C:\Users\mtepe\OneDrive\Masaüstü\react-native\firstProject\node_modules@react-native-community\cli\build\bin.js, config]' command failed.
BU�LD FAILED in 2s
at makeError (C:\Users\mtepe\OneDrive\Masaüstü\react-native\firstProject\node_modules@react-native-community\cli-platform-android\node_modules\execa\index.js:174:9)
at C:\Users\mtepe\OneDrive\Masaüstü\react-native\firstProject\node_modules@react-native-community\cli-platform-android\node_modules\execa\index.js:278:16
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async runOnAllDevices (C:\Users\mtepe\OneDrive\Masaüstü\react-native\firstProject\node_modules@react-native-community\cli-platform-android\build\commands\runAndroid\runOnAllDevices.js:109:5)
at async Command.handleAction (C:\Users\mtepe\OneDrive\Masaüstü\react-native\firstProject\node_modules@react-native-community\cli\build\index.js:142:9)
info Run CLI with --verbose flag for more details.
已反复按照React Native 0.70官方环境搭建指南操作,仍无法解决。
解决方案
1. 修复依赖安装完整性
- 删除项目根目录下的
node_modules文件夹和package-lock.json文件 - 执行
npm install重新安装所有依赖,确保安装过程无报错 - 若存在网络问题,可切换npm源:
npm config set registry https://registry.npmmirror.com/
2. 手动安装匹配版本的cli
React Native 0.70对应@react-native-community/cli的8.x版本,执行以下命令手动安装:
npm install @react-native-community/cli@8.0.0 --save-dev
3. 迁移项目到无特殊字符路径
从错误路径可见,项目所在路径包含非ASCII字符Masaüstü,Gradle和Node.js对这类路径的支持存在兼容性问题:
- 将项目移动到不含特殊字符、空格的路径,比如
C:\react-native\firstProject - 重新执行
npm run android
4. 清理Gradle缓存
- 打开Android Studio,点击
File > Invalidate Caches...,选择Invalidate and Restart - 或手动删除项目根目录下的
.gradle文件夹和android/.gradle文件夹 - 重新执行
npm run android
5. 验证Node.js版本兼容性
React Native 0.70推荐Node.js 16.x或18.x版本,若当前版本仍有问题,可尝试切换到16.x稳定版:
# 使用nvm管理版本 nvm install 16.20.0 nvm use 16.20.0
内容的提问来源于stack exchange,提问作者baristp04

