项目中@shopify/cli-kit依赖React17.0.2,如何更新其版本?
问题描述
运行npm ls react后得到以下依赖树:
PS > npm ls react ├─┬ @headlessui/react@1.7.15 │ └── react@18.2.0 deduped ├─┬ @heroicons/react@2.0.18 │ └── react@18.2.0 deduped ├─┬ @mux/mux-video-react@0.7.14 │ └── react@18.2.0 deduped ├─┬ @n8tb1t/use-scroll-position@2.0.3 │ └── react@18.2.0 deduped ├─┬ @portabletext/react@2.0.2 │ └── react@18.2.0 deduped ├─┬ @react-google-maps/api@2.18.1 │ └── react@18.2.0 deduped ├─┬ @sanity/block-content-to-react@3.0.0 │ └── react@18.2.0 deduped ├─┬ @shopify/cli-hydrogen@3.39.0 │ ├─┬ @shopify/cli-kit@3.39.0 │ │ ├─┬ ink@3.2.0 │ │ │ ├─┬ react-reconciler@0.26.2 │ │ │ │ └── react@18.2.0 deduped invalid: "^17.0.2" from node_modules/ink/node_modules/react-reconciler │ │ │ └── react@18.2.0 deduped │ │ └── react@17.0.2 │ └─┬ @shopify/hydrogen@0.26.0 │ └── react@18.2.0 deduped ├─┬ @shopify/cli@3.44.1 │ ├─┬ @shopify/cli-kit@3.44.1 │ │ └── react@17.0.2 │ └─┬ @shopify/plugin-did-you-mean@3.44.1 │ └─┬ @shopify/cli-kit@3.44.1 │ └── react@17.0.2 ├─┬ @shopify/hydrogen@1.6.6 │ ├─┬ @xstate/react@3.0.1 │ │ ├── react@18.2.0 deduped │ │ └─┬ use-isomorphic-layout-effect@1.1.2 │ │ └── react@18.2.0 deduped │ ├─┬ react-error-boundary@3.1.4 │ │ └── react@18.2.0 deduped │ ├─┬ react-helmet-async@1.3.0 │ │ └── react@18.2.0 deduped │ └── react@18.2.0 deduped ├─┬ @tippyjs/react@4.2.6 │ └── react@18.2.0 deduped ├─┬ @types/react-datepicker@4.11.2 │ └─┬ react-popper@2.3.0 │ └── react@18.2.0 deduped ├─┬ embla-carousel-react@7.1.0 │ └── react@18.2.0 deduped ├─┬ react-cookie@4.1.1 │ └── react@18.2.0 deduped ├─┬ react-datepicker@4.15.0 │ ├─┬ react-onclickoutside@6.12.2 │ │ └── react@18.2.0 deduped │ └── react@18.2.0 deduped ├─┬ react-dom@18.2.0 │ └── react@18.2.0 deduped ├─┬ react-ga@3.3.1 │ └── react@18.2.0 deduped ├─┬ react-hook-form@7.45.1 │ └── react@18.2.0 deduped ├─┬ react-icons@4.10.1 │ └── react@18.2.0 deduped ├─┬ react-instantsearch-hooks-web@6.45.1 │ ├─┬ react-instantsearch-hooks@6.45.1 │ │ └── react@18.2.0 deduped │ └── react@18.2.0 deduped ├─┬ react-map-gl@7.1.2 │ └── react@18.2.0 deduped ├─┬ react-social-media-embed@2.3.5 │ ├─┬ react-sub-unsub@2.1.10 │ │ └── react@18.2.0 deduped │ ├─┬ react-twitter-embed@4.0.4 │ │ └── react@18.2.0 deduped │ ├─┬ react-youtube@10.1.0 │ │ └── react@18.2.0 deduped │ └── react@18.2.0 deduped ├─┬ react-type-animation@2.1.2 │ └── react@18.2.0 deduped ├── react@18.2.0 └─┬ swr@2.2.0 ├── react@18.2.0 deduped └─┬ use-sync-external-store@1.2.0 └── react@18.2.0 deduped
项目中大部分依赖都使用react@18.2.0,仅@shopify/cli-kit依赖react@17.0.2,想知道:
- 是否可以更新
@shopify/cli-kit的React版本? - 如何正确更新项目中的React版本?
解答
关于@shopify/cli-kit的React版本更新
首先明确:@shopify/cli-kit是@shopify/cli和@shopify/cli-hydrogen的依赖,属于开发工具类依赖,不会被打包到生产环境代码中,所以它的React版本理论上不会影响项目生产运行。
可行的更新方式
- 升级cli-kit及关联包:先查看
@shopify/cli-kit的最新版本,确认其peerDependencies是否已支持React 18。如果支持,直接升级相关包到最新版:
这种方式最稳妥,官方更新通常会解决兼容性问题。npm install @shopify/cli@latest @shopify/cli-hydrogen@latest @shopify/cli-kit@latest --save-dev - 强制覆盖依赖版本:如果暂时无法升级cli-kit,可通过npm的
overrides字段(或yarn的resolutions)强制让cli-kit使用React 18。在package.json中添加:
然后执行"overrides": { "@shopify/cli-kit": { "react": "^18.2.0", "react-dom": "^18.2.0" } }npm install。但这种方式有风险,可能导致cli工具运行出错,建议先测试开发命令(比如npm run dev、npm run build)是否正常。
项目React版本更新步骤(若需升级到最新版)
你的项目已经使用react@18.2.0,以下是通用的更新维护步骤:
- 检查依赖兼容性:确认项目核心依赖(如
react-dom、路由库、状态管理库等)均支持目标React版本,避免出现兼容性问题。 - 执行更新命令:
# 更新react和react-dom到最新稳定版 npm install react@latest react-dom@latest # 若使用TypeScript,更新类型定义 npm install @types/react@latest @types/react-dom@latest --save-dev - 清理依赖并重新安装:
也可以用# 删除node_modules和锁文件 rm -rf node_modules package-lock.json # 重新安装依赖 npm installnpm clean-install替代上述步骤,快速清理并重装。 - 修复代码兼容性问题:React版本升级可能带来API废弃或行为变化(比如React 18中
ReactDOM.render被createRoot替代),运行项目后根据控制台警告/错误修复对应代码。 - 测试验证:运行开发服务器、执行生产构建,确保项目功能正常,无报错或异常行为。
内容的提问来源于stack exchange,提问作者jerma lover1
相关产品推荐
相关产品推荐

