Rollup failed to resolve import "element-plus/es/components"
vite打包异常,错误信息如下:
[vite]: Rollup failed to resolve import "element-plus/es/components" from "node_modules/element-plus/es/index.js". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to `build.rollupOptions.external` error during build: Error: [vite]: Rollup failed to resolve import "element-plus/es/components" from "node_modules/element-plus/es/index.js". This is most likely unintended because it can break your application at runtime. If you do want to externalize this module explicitly add it to `build.rollupOptions.external` at onRollupWarning (/Users/wuqian-mac-mini/Desktop/wuqianWork/nwdGitlab/cxqdjf/yz/change-wechat-web/node_modules/vite/dist/node/chunks/dep-6c23b6f8.js:51247:19) at onwarn (/Users/wuqian-mac-mini/Desktop/wuqianWork/nwdGitlab/cxqdjf/yz/change-wechat-web/node_modules/vite/dist/node/chunks/dep-6c23b6f8.js:51032:13) at Object.onwarn (/Users/wuqian-mac-mini/Desktop/wuqianWork/nwdGitlab/cxqdjf/yz/change-wechat-web/node_modules/rollup/dist/shared/rollup.js:23216:13) at ModuleLoader.handleResolveId (/Users/wuqian-mac-mini/Desktop/wuqianWork/nwdGitlab/cxqdjf/yz/change-wechat-web/node_modules/rollup/dist/shared/rollup.js:22466:26) at /Users/wuqian-mac-mini/Desktop/wuqianWork/nwdGitlab/cxqdjf/yz/change-wechat-web/node_modules/rollup/dist/shared/rollup.js:22427:26
解决,在 vite.config.js 里添加:
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import path from 'path' import Components from 'unplugin-vue-components/vite'; import { VantResolver } from 'unplugin-vue-components/resolvers'; export default defineConfig({ plugins: [ vue(), Components({ resolvers: [VantResolver()], }), ], resolve: { alias: [ { find: '@', replacement: path.resolve(__dirname, 'src') } ] }, build: { rollupOptions: { external: [ "element-plus" ] } } })
发布要求字数:凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数