npm plugin:vite:import-analysis无法解析包“xxx”的条目,包可能在package.json中指定了不正确的main/module/exports

qxgroojn  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(618)

我的npm包here有一个错误的构建(使用vite),当我在一个项目中安装它时,它在标题中显示错误。
整个错误信息:

] Failed to resolve entry for package "vue3-drag-resize-rotate". The package may have incorrect main/module/exports specified in its package.json. [plugin vite:dep-scan]

    node_modules/.pnpm/esbuild@0.17.18/node_modules/esbuild/lib/main.js:1360:21:
      1360 │         let result = await callback({
           ╵                      ^

    at packageEntryFailure (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:23384:11)
    at resolvePackageEntry (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:23381:5)
    at tryNodeResolve (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:23115:20)
    at Context.resolveId (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:22876:28)
    at Object.resolveId (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:42811:46)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async resolve (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:43109:26)
    at async file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:43286:34
    at async requestCallbacks.on-resolve (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:1360:22)
    at async handleRequest (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:723:13)

  This error came from the "onResolve" callback registered here:

    node_modules/.pnpm/esbuild@0.17.18/node_modules/esbuild/lib/main.js:1279:20:
      1279 │       let promise = setup({
           ╵                     ^

    at setup (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:43276:19)
    at handlePlugins (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:1279:21)
    at buildOrContextImpl (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:968:5)
    at Object.buildOrContext (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:776:5)
    at D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:2172:68
    at new Promise (<anonymous>)
    at Object.context (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:2172:27)
    at Object.context (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:2012:58)
    at prepareEsbuildScanner (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:43061:26)

  The plugin "vite:dep-scan" was triggered by this import

    script:D:/program/own/test-xmov-component/src/components/HelloWorld.vue?id=0:8:7:
      8 │ import 'vue3-drag-resize-rotate'
        ╵        ~~~~~~~~~~~~~~~~~~~~~~~~~

    at failureErrorWithLog (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:1636:15)
    at D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:1048:25
    at runOnEndCallbacks (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:1471:45)
    at buildResponseToResult (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:1046:7)
    at D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:1058:9
    at new Promise (<anonymous>)
    at requestCallbacks.on-end (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:1057:54)
    at handleRequest (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:723:19)
    at handleIncomingPacket (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:745:7)
    at Socket.readFromStdout (D:\program\own\test-xmov-component\node_modules\.pnpm\esbuild@0.17.18\node_modules\esbuild\lib\main.js:673:7)
Failed to resolve entry for package "vue3-drag-resize-rotate". The package may have incorrect main/module/exports specified in its package.json.
下午11:46:05 [vite] Internal server error: Failed to resolve entry for package "vue3-drag-resize-rotate". The package may have incorrect main/module/exports specified in its package.json.
  Plugin: vite:import-analysis
  File: D:/program/own/test-xmov-component/src/components/HelloWorld.vue
      at packageEntryFailure (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:23384:11)
      at resolvePackageEntry (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:23381:5)
      at tryNodeResolve (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:23115:20)
      at Context.resolveId (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:22876:28)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async Object.resolveId (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:42811:32)
      at async TransformContext.resolve (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:42539:23)
      at async normalizeUrl (file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:40502:34)
      at async file:///D:/program/own/test-xmov-component/node_modules/.pnpm/vite@4.3.5_@types+node@20.1.2/node_modules/vite/dist/node/chunks/dep-934dbc7c.js:40653:47
      at async Promise.all (index 1)

此外,我还把项目与打字脚本推理捆绑在一起。我想它可以导出示例的类型和其他类型,但我失败了。
谢谢您的帮助。
我设置vite.config.ts文件:

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  build: {
    outDir: 'lib',
    lib: {
      entry: 'packages/index.ts',
      name: 'vue3-drag-resize-rotate',
      fileName: 'vue3-drag-resize-rotate',
      formats: ['es', 'cjs', 'umd'],
    },
    rollupOptions: {
      external: ['vue'],
      input: 'packages/index.ts',
      output: {
        globals: {
          vue: 'Vue',
        },
      },
    },
  },
})

下面是这个包。json:

{
  "name": "vue3-drag-resize-rotate",
  "version": "0.0.8",
  "type": "module",
  "types": "packages/components/drr.vue.d.ts",
  "main": "lib/vue3-drag-resize-rotate.umd.js",
  "module": "lib/vue3-drag-resize-rotate.mjs",
  "unpkg": "lib/vue3-drag-resize-rotate.umd.js",
  "jsdelivr": "lib/vue3-drag-resize-rotate.umd.js",
  "repository": {
    "type": "git",
    "url": "https://github.com/AvailableForTheWorld/vue3-drag-resize-rotate"
  },
  "bugs": {
    "url": "https://github.com/AvailableForTheWorld/vue3-drag-resize-rotate/issues"
  },
  "exports": {
    ".": {
      "require": "./lib/vue3-drag-resize-rotate.umd.js",
      "import": "./lib/vue3-drag-resize-rotate.mjs"
    },
    "./package.json": "./package.json",
    "./lib/*": "./lib/*"
  },
  "files": [
    "lib",
    "packages"
  ],
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "nanoid": "^4.0.2",
    "vue": "^3.2.47"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.1.0",
    "typescript": "^5.0.2",
    "vite": "^4.3.2",
    "vue-tsc": "^1.4.2"
  }
}

不管用,我不知道为什么

hec6srdp

hec6srdp1#

我也有类似的问题,请确保在您的dist文件夹中,您生成的是一个.umd.js文件,而不是.umd.cjs文件。
对我来说,更新package.json以指向.cjs文件就做到了这一点。

"main": "./dist/package-name.umd.cjs",
"exports": {
  ".": {
    "import": "./dist/package-name.js",
    "require": "./dist/package-name.umd.cjs"
  }
},

相关问题