报错信息:

vite v6.3.6 building for production... ✓ 1646 modules transformed. ✗ Build failed in 13.09s error during build: [vite]: Rollup failed to resolve import "node_modules/axios/index.cjs" from "D:/MyCode/wylims-jm/lims-client/src/views/capital/CapitalAllocate.vue?vue&type=script&setup=true&lang.ts". 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 viteLog (file:///D:/MyCode/wylims-jm/lims-client/node_modules/vite/dist/node/chunks/dep-Bu492Fnd.js:46363:15) at onRollupLog (file:///D:/MyCode/wylims-jm/lims-client/node_modules/vite/dist/node/chunks/dep-Bu492Fnd.js:46413:5) at onLog (file:///D:/MyCode/wylims-jm/lims-client/node_modules/vite/dist/node/chunks/dep-Bu492Fnd.js:46061:7) at file:///D:/MyCode/wylims-jm/lims-client/node_modules/rollup/dist/es/shared/node-entry.js:20803:32 at Object.logger [as onLog] (file:///D:/MyCode/wylims-jm/lims-client/node_modules/rollup/dist/es/shared/node-entry.js:22683:9) at ModuleLoader.handleInvalidResolvedId (file:///D:/MyCode/wylims-jm/lims-client/node_modules/rollup/dist/es/shared/node-entry.js:21429:26) at file:///D:/MyCode/wylims-jm/lims-client/node_modules/rollup/dist/es/shared/node-entry.js:21387:26 ERROR: "build-only" exited with 1. PS D:\MyCode\wylims-jm\lims-client>

vue3 vite打包报错:
vite v6.3.6 building for production...
✓ 1646 modules transformed.
✗ Build failed in 13.09s
error during build:
[vite]: Rollup failed to resolve import "node_modules/axios/index.cjs" from "D:/MyCode/wylims-jm/lims-client/src/views/capital/CapitalAllocate.vue?vue&type=script&setup=true&lang.ts".
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 viteLog (file:///D:/MyCode/wylims-jm/lims-client/node_modules/vite/dist/node/chunks/dep-Bu492Fnd.js:46363:15)   
    at onRollupLog (file:///D:/MyCode/wylims-jm/lims-client/node_modules/vite/dist/node/chunks/dep-Bu492Fnd.js:46413:5)
    at onLog (file:///D:/MyCode/wylims-jm/lims-client/node_modules/vite/dist/node/chunks/dep-Bu492Fnd.js:46061:7)      
    at file:///D:/MyCode/wylims-jm/lims-client/node_modules/rollup/dist/es/shared/node-entry.js:20803:32
    at Object.logger [as onLog] (file:///D:/MyCode/wylims-jm/lims-client/node_modules/rollup/dist/es/shared/node-entry.js:22683:9)
    at ModuleLoader.handleInvalidResolvedId (file:///D:/MyCode/wylims-jm/lims-client/node_modules/rollup/dist/es/shared/node-entry.js:21429:26)
    at file:///D:/MyCode/wylims-jm/lims-client/node_modules/rollup/dist/es/shared/node-entry.js:21387:26
ERROR: "build-only" exited with 1.
PS D:\MyCode\wylims-jm\lims-client>

你在打包时遇到的错误是因为在 CapitalAllocate.vue 文件中,有一行导入语句使用了错误的路径

ts

import ... from 'node_modules/axios/index.cjs'

Vite / Rollup 无法解析这个路径,因为正确的导入方式应该使用包名,而不是指向 node_modules 内部的物理文件路径。

解决方案

1. 修改错误的导入语句(推荐)

打开 src/views/capital/CapitalAllocate.vue,找到类似下面的代码:

ts

// ❌ 错误写法
import axios from 'node_modules/axios/index.cjs'
// 或
import * as axios from 'node_modules/axios/index.cjs'

改为:

ts

// ✅ 正确写法
import axios from 'axios'

如果你的代码中使用了其他 axios 相关的具名导入,也一并改为标准方式:

ts

import axios, { AxiosInstance, AxiosResponse } from 'axios'

2. 确认 axios 已安装

在项目根目录执行:

bash

npm install axios
# 或
yarn add axios

3. 清理缓存并重新打包

bash

# 删除 node_modules/.vite 缓存
rm -rf node_modules/.vite

# 重新打包
npm run build

如果你确实需要导入 .cjs 文件(不推荐)

某些特殊场景下(如使用 CommonJS 版本的 axios),你可以通过 Vite 配置来允许这种导入,但不推荐,因为会破坏 ES 模块的最佳实践。

如果坚持这样做,可以修改 vite.config.ts

ts

import { defineConfig } from 'vite'

export default defineConfig({
  build: {
    rollupOptions: {
      external: ['node_modules/axios/index.cjs']  // 告诉 Rollup 不要解析它
    }
  },
  // 同时需要为开发服务器添加别名
  resolve: {
    alias: {
      'node_modules/axios/index.cjs': 'axios'
    }
  }
})

但这样只是绕过了错误,并没有真正解决问题,依然可能导致运行时错误。强烈建议使用第一种方案,改正导入语句。


补充:为什么会出现这种错误?

  • 可能是有人手动复制了物理路径,或者某些自动导入工具生成了错误的路径。

  • Vite 在处理 node_modules 中的文件时,期望通过包名(如 'axios')来解析,而不是直接写文件系统路径。

Logo

AtomGit 是由开放原子开源基金会联合 CSDN 等生态伙伴共同推出的新一代开源与人工智能协作平台。平台坚持“开放、中立、公益”的理念,把代码托管、模型共享、数据集托管、智能体开发体验和算力服务整合在一起,为开发者提供从开发、训练到部署的一站式体验。

更多推荐