originjs / vite-plugin-federation

Module Federation for vite & rollup
Other
2.3k stars 236 forks source link

Error: Missing "./package.json" specifier in "lit-html" package #357

Open Artur- opened 1 year ago

Artur- commented 1 year ago

Versions

Reproduction

cd packages/examples/vue3-demo-esm/layout
pnpm i lit-html
# Change to `shared: ["lit-html"]` in vite.config.ts
pnpm build

What is Expected?

The project builds

What is actually happening?

Error: Missing "./package.json" export in "lit-html" package
    at bail (file:///.../vite-plugin-federation/node_modules/.pnpm/vite@4.0.3/node_modules/vite/dist/node/chunks/dep-0bae2027.js:14784:8)
    at resolve$1 (file:///.../vite-plugin-federation/node_modules/.pnpm/vite@4.0.3/node_modules/vite/dist/node/chunks/dep-0bae2027.js:14861:10)
    at resolveExports (file:///.../vite-plugin-federation/node_modules/.pnpm/vite@4.0.3/node_modules/vite/dist/node/chunks/dep-0bae2027.js:21850:12)
    at resolveDeepImport (file:///.../vite-plugin-federation/node_modules/.pnpm/vite@4.0.3/node_modules/vite/dist/node/chunks/dep-0bae2027.js:21868:31)
    at tryNodeResolve (file:///.../vite-plugin-federation/node_modules/.pnpm/vite@4.0.3/node_modules/vite/dist/node/chunks/dep-0bae2027.js:21557:20)
    at Object.resolveId (file:///.../vite-plugin-federation/node_modules/.pnpm/vite@4.0.3/node_modules/vite/dist/node/chunks/dep-0bae2027.js:21321:28)
    at file:///.../vite-plugin-federation/node_modules/.pnpm/rollup@3.9.1/node_modules/rollup/dist/es/shared/rollup.js:23660:40 {
  code: 'PLUGIN_ERROR',
  plugin: 'commonjs--resolver',
  hook: 'resolveId'
}

It is not at all clear what the problem is or what to check so I have no idea what to do next

Artur- commented 1 year ago

Seems to be related to lit-html having an export map and possibly this plugin trying to read package.json by importing it from the package, which is not allowed

Artur- commented 1 year ago

Seems like Vite uses

import resolve from 'resolve'
...
resolve.sync(id)

for this and that one can properly resolve lit-html/package.json

https://github.com/vitejs/vite/blob/f3a0c3b72c3483808d5649efbcc8118927a9d0b4/packages/vite/src/node/utils.ts#L140-L153

ruleeeer commented 1 year ago

image It seems that package.json cannot be read because of this reason, the export in the package.json file is not defined.

ruleeeer commented 1 year ago

A version option can be added to eliminate this problem,

shared:{
  "lit-html": {
           version: "^2.6.1"
        }
}