使用npx create-react-native-library创建React Native库失败求助
问题:使用create-react-native-library创建RN库时出现"Invalid Version: latest"错误
我尝试用npx create-react-native-library qrscanlib创建React Native库,完成作者名称、仓库地址等配置后,触发了如下错误:
✔ Which languages do you want to use? › Java & Objective-C ⠧ Generating example app ⚠️ The `init` command is deprecated. The behavior will be changed on 12/31/2024 (82 days). - Switch to npx @react-native-community/cli init for the identical behavior. - Refer to the documentation for information about alternative tools: https://reactnative.dev/docs/getting-started Running: npx @react-native-community/cli init error Invalid Version: latest. /Users/rj/.npm/_npx/7655a6858458ff34/node_modules/create-react-native-library/lib/utils/spawn.js:27 reject(new Error(stderr.trim())); ^ Error: ⚠️ The `init` command is deprecated. The behavior will be changed on 12/31/2024 (82 days). - Switch to npx @react-native-community/cli init for the identical behavior. - Refer to the documentation for information about alternative tools: https://reactnative.dev/docs/getting-started Running: npx @react-native-community/cli init error Invalid Version: latest. at ChildProcess.<anonymous> (/Users/rj/.npm/_npx/7655a6858458ff34/node_modules/create-react-native-library/lib/utils/spawn.js:27:16) at Object.onceWrapper (node:events:628:26) at ChildProcess.emit (node:events:513:28) at maybeClose (node:internal/child_process:1091:16) at ChildProcess._handle.onexit (node:internal/child_process:302:5)
临时修复方案
- 指定RN版本手动创建示例项目:放弃工具自动生成example,手动执行
npx @react-native-community/cli init example --version 0.72.6(替换为你需要的稳定RN版本,比如0.7x系列最新稳定版),之后将该example目录与工具生成的库核心代码关联,修改库根目录package.json中的scripts命令,指向example项目的运行路径。 - 降级create-react-native-library版本:使用旧版本工具规避版本参数问题,执行
npx create-react-native-library@3.1.0 qrscanlib,旧版本未适配最新RN CLI的版本校验逻辑,可正常生成项目。 - 补全初始化流程:若工具已生成库的核心代码(android/ios/src等目录),手动补全example项目:
- 在库根目录创建
example文件夹 - 进入example执行
npx @react-native-community/cli init . --version <具体版本号> - 在example/package.json中添加本地库依赖:
"qrscanlib": "file:.." - 执行
npm install,按RN库开发流程完成自动或手动链接配置
- 在库根目录创建
替代创建方式
- 手动搭建RN库结构:
- 创建项目根目录,执行
npm init -y初始化package.json - 创建
android、ios、src目录,分别存放原生代码和JS逻辑 - 参考成熟RN库的配置,手动设置package.json中的
main、types、scripts等字段 - 自行创建example项目并关联本地库进行测试
- 创建项目根目录,执行
内容的提问来源于stack exchange,提问作者Rajendra
相关产品推荐
相关产品推荐

