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

使用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项目:
    1. 在库根目录创建example文件夹
    2. 进入example执行npx @react-native-community/cli init . --version <具体版本号>
    3. 在example/package.json中添加本地库依赖:"qrscanlib": "file:.."
    4. 执行npm install,按RN库开发流程完成自动或手动链接配置

替代创建方式

  • 手动搭建RN库结构:
    1. 创建项目根目录,执行npm init -y初始化package.json
    2. 创建android、ios、src目录,分别存放原生代码和JS逻辑
    3. 参考成熟RN库的配置,手动设置package.json中的main、types、scripts等字段
    4. 自行创建example项目并关联本地库进行测试

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 11:23:19