pinia: Cannot read properties of undefined (reading '_s')

发布时间 2023-10-24 21:47:03作者: 死宅程序员

使用 Vue3 + Pinia + PNPM + Vite 开发一个前端项目时,运行preview,报错:

pinia Cannot read properties of undefined (reading '_s')

报错的代码是压缩后的:

function we(e, t, n) {
  let s, c;
  const u = typeof t == "function";
  typeof e == "string" ? ((s = e), (c = u ? n : t)) : ((c = e), (s = e.id));
  function a(o, h) {
    const f = ie();
    return (
      (o = o || (f ? ue(be, null) : null)),
      o && F(o),
      (o = H),
      o._s.has(s) || (u ? V(s, t, c, o) : ve(s, c, o)), // 报错位置
      o._s.get(s)
    );
  }
  return (a.$id = s), a;
}

大意像是读取一个 store的名称失败导致。而我确认这个 store 已经被注册(defineStore)了。
翻阅 pinia 源码,定位到位置:https://github.com/vuejs/pinia/blob/5c94d26e11f8d2ed0e01d5a615f8e014d799b4a0/packages/pinia/src/store.ts#L895-L966 ,不过依然不明确问题原因。

突然灵感乍现,有新发现,我这个工程是 pnpm workspace, 两个包都依赖了 pinia,但版本不一致了。统一版本后再 build,问题解决。

那么,怎么保证不同的包的相同依赖版本一致呢?pnpm 给出了方案:https://pnpm.io/package_json#pnpmoverrides