解决 vue2.7使用 pnpm 和 pinia 2.x报错

看了很多帖子,都说和vue2.7 不兼容,需要降级到2.0.x,但还是没有效果,pinia 2.x的最后更新版本也支持2.7,使用yarn可以运行,使用pnpm就报错

github.com/vuejs/pinia...

md 复制代码
2.3.1 (2025-01-20)
Bug Fixes
types: support for Vue 2.7 (d14e1a7)

环境说明

package.json

json 复制代码
 "dependencies": {
    "pinia": "2.3.1",
    "vue": "2.7.16"
  }

报错信息

报错关键字:export 'hasInjectionContext' (imported as 'hasInjectionContext') 报错信息:

sh 复制代码
error   Build errors: 
  ./node_modules/.pnpm/pinia@2.3.1_vue@2.7.16/node_modules/pinia/dist/pinia.mjs ×

File: ./node_modules/.pnpm/pinia@2.3.1_vue@2.7.16/node_modules/pinia/dist/pinia.mjs:1726:27-46
  × ESModulesLinkingError: export 'hasInjectionContext' (imported as 'hasInjectionContext') was not found in 'vue-demi' (possible exports: EffectScope, Vue, Vue2, computed, customRef, defineAsyncComponent, defineComponent, del, effectScope, getCurrentInstance, getCurrentScope, h, inject, install, isProxy, isReactive, isReadonly, isRef, isShallow, isVue2, isVue3, markRaw, mergeDefaults, nextTick, onActivated, onBeforeMount, onBeforeUnmount, onBeforeUpdate, onDeactivated, onErrorCaptured, onMounted, onRenderTracked, onRenderTriggered, onScopeDispose, onServerPrefetch, onUnmounted, onUpdated, provide, proxyRefs, reactive, readonly, ref, set, shallowReactive, shallowReadonly, shallowRef, toRaw, toRef, toRefs, triggerRef, unref, useAttrs, useCssModule, useCssVars, useListeners, useSlots, version, watch, watchEffect, watchPostEffect, watchSyncEffect)
      ╭─[1726:27]
 1724 │     }
 1725 │     function useStore(pinia, hot) {
 1726 │         const hasContext = hasInjectionContext();
      ·                            ───────────────────
 1727 │         pinia =
 1728 │             // in test mode, ignore the argument provided as we can always retrieve a
      ╰────
Import traces (entry → error):

问题分析

由于 vue-demi 被 Ignored build scripts(忽略了构建脚本),它无法执行安装后的 postinstall 脚本。

为什么这会导致报错? vue-demi 是 Pinia 兼容 Vue 2 和 Vue 3 的核心桥梁。它在安装时需要运行一个脚本来检测你当前项目是 Vue 2 还是 Vue 3,并据此"切换"导出的 API。

因为 pnpm 默认禁用了未授权的脚本运行,vue-demi 没能成功切换。

结果它默认指向了 Vue 3 的导出路径,导致 Pinia 去调用 Vue 2.7 根本不存在的 hasInjectionContext,从而引发了你看到的错误。

解决办法

授权并重新构建 在终端运行以下命令:

sh 复制代码
pnpm approve-builds

空格选中 vue-demi

sh 复制代码
pnpm approve-builds
√ Choose which packages to build (Press <space> to select, <a> to toggle all, <i> to invert selection) · vue-demi
√ The next packages will now be built: vue-demi.
Do you approve? (y/N) · true
node_modules/.pnpm/vue-demi@0.14.10_vue@2.7.16/node_modules/vue-demi: Running postinstall script, done in 127ms

再次执行pnpm dev 即可

相关推荐
weixin_427771614 分钟前
前端调试隐藏元素
前端
爱上好庆祝1 小时前
学习js的第五天
前端·css·学习·html·css3·js
C澒1 小时前
IntelliPro 产研协作平台:基于 AI Agent 的低代码智能化配置方案设计与实现
前端·低代码·ai编程
一袋米扛几楼981 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
网络点点滴2 小时前
前端与后端的区别与联系
前端
EnCi Zheng2 小时前
M5-markconv自定义CSS样式指南 [特殊字符]
前端·css·python
kyriewen2 小时前
你的网页慢,用户不说直接走——前端性能监控教你“读心术”
前端·性能优化·监控
广州华水科技2 小时前
北斗GNSS变形监测在大坝安全监测中的应用与优势分析
前端
前端老石人2 小时前
前端开发中的 URL 完全指南
开发语言·前端·javascript·css·html
CAE虚拟与现实2 小时前
五一假期闲来无事,来个前段、后端的说明吧
前端·后端·vtk·three.js·前后端