plugin:vite:import-analysis Failed to resolve entry for package "xxx". The package may have incorrect main/module/exports specified in package.json

huangapple go评论91阅读模式
英文:

plugin:vite:import-analysis Failed to resolve entry for package "xxx". The package may have incorrect main/module/exports specified in package.json

问题

Your npm package seems to have a build issue when using Vite, and it's showing an error related to resolving the package entry. Additionally, you're encountering problems bundling the project with TypeScript inference.

To resolve the build issue, you should check your package.json file and ensure that you've correctly specified the "main" and "module" fields to point to the appropriate entry files. Also, make sure that your TypeScript configuration is set up correctly for the project.

For TypeScript inference issues, you might need to ensure that your package exports the necessary types correctly. You can define the types in your TypeScript declaration file (d.ts) and ensure that they are exported from the package. Double-check the path to your types in your package.json file.

If you need further assistance with specific issues or error messages, please provide more details about the exact problems you are facing, and I'll do my best to help you troubleshoot.

英文:

my npm package here has a wrong build (using vite), when I install it in a project, it shows the error in the title.

the whole error info:

] 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)

and further, I have troble bundling the project with typescript inference. I want to it can export the type of the instance and other types, but I failed.

thanks a lot for helping me.

I set the vite.config.ts file:

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',
        },
      },
    },
  },
})

here is the package.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"
  }
}

it didn't work , I don't know why

答案1

得分: 1

在你的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"
  }
}
英文:

I had a similar issue, make sure that in your dist folder, what you have generated is really a .umd.js file and not a .umd.cjs file.

For me updating the package.json to point to the .cjs file did the trick.

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

huangapple
  • 本文由 发表于 2023年5月11日 00:11:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定