vue-mini / create-vue-mini

🛠️ 创建 Vue Mini 小程序项目的最佳方案。
MIT License
41 stars 14 forks source link

如果 npm package 的 package.json 里有 exports, 并且里面没有 ./package.json key,build.js 就会报错 #19

Closed CGQAQ closed 2 months ago

CGQAQ commented 2 months ago

如果 npm package 的 package.json 里有 exports, 并且里面没有 ./package.json key,build.js 就会报错

import.meta.resolve(`${module}/package.json`) // 如果 package.json 里有exports,并且没有包含 `./package.json` 就会报错
node:internal/modules/esm/resolve:304
  return new ERR_PACKAGE_PATH_NOT_EXPORTED(
         ^

Error [ERR_PACKAGE_PATH_NOT_EXPORTED]: Package subpath './package.json' is not defined by "exports" in D:\Code\Tmp\xxx\node_modules\lodash\package.json imported from D:\Code\Tmp\xxx\build.js
    at exportsNotFound (node:internal/modules/esm/resolve:304:10)
    at packageExportsResolve (node:internal/modules/esm/resolve:651:9)
    at packageResolve (node:internal/modules/esm/resolve:837:14)
    at moduleResolve (node:internal/modules/esm/resolve:927:18)
    at defaultResolve (node:internal/modules/esm/resolve:1157:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:390:12)
    at ModuleLoader.resolveSync (node:internal/modules/esm/loader:372:17)
    at Object.resolve (node:internal/modules/esm/initialize_import_meta:30:25)
    at buildComponentLibrary (file:///D:/Code/Tmp/xxx/build.js:101:25)
    at scanDependencies (file:///D:/Code/Tmp/xxx/build.js:158:21) {

具体报错是这里: https://github.com/nodejs/node/blob/9db6327af3272c4125d20c078010600629506a26/lib/internal/modules/esm/resolve.js#L586

yangmingshan commented 2 months ago

是的。这个在 #16 中有讨论,目前的结论是需要 package 修改。建议你去对应的 package 提 issue 或者 PR。

CGQAQ commented 2 months ago

是什么原因导致不能这么写呢?


const pkg = await fs.readJson(
+  fileURLToPath(new URL("./package.json", import.meta.resolve(`lodash`))),
-  fileURLToPath(new URL(import.meta.resolve(`lodash/package.json`))),
  "utf8"
);
yangmingshan commented 2 months ago

import.meta.resolve('package') 返回的是入口文件的路径,不是 package 根路径。