使用react-native-community/cli自定义模板时出现ENOENT错误求助
问题描述
尝试通过CLI使用自制React Native模板创建项目时执行命令报错,react-native@latest init可正常运行但将被弃用,希望使用@react-native-community/cli@latest init实现需求。
执行命令
npx react-native-community/cli@latest init MyApp --template file:///E:/React%20Native/rnboilerplate
报错信息
npm error code ENOENT npm error syscall open npm error path E:\React Native\RNTesting\react-native-community\cli@latest\package.json npm error errno -4058 npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open 'E:\React Native\RNTesting\react-native-community\cli@latest\package.json' npm error enoent This is related to npm not being able to find a file. npm error enoent npm error A complete log of this run can be found in: C:\Users\User\AppData\Local\npm-cache\_logs\2024-08-20T03_37_29_814Z-debug-0.log
解决方案
- 修正CLI包名:官方的React Native社区CLI包名是
@react-native-community/cli,而非react-native-community/cli,执行正确命令:npx @react-native-community/cli@latest init MyApp --template file:///E:/React%20Native/rnboilerplate - 检查本地模板结构:确保自制模板目录
E:/React Native/rnboilerplate下存在package.json,且包含React Native CLI模板所需的配置文件(如template.config.js或template.json),符合模板规范。 - 清理npm缓存:若仍存在文件查找问题,执行缓存清理后重试:
npm cache clean --force - 全局安装CLI后运行:先全局安装最新版社区CLI,再执行初始化命令:
npm install -g @react-native-community/cli@latest react-native init MyApp --template file:///E:/React%20Native/rnboilerplate
内容的提问来源于stack exchange,提问作者Newbeees
相关产品推荐
相关产品推荐

