You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vite执行build命令报错:src/main.ts未导出default求解决

Vite v4.5.0构建报错:"default" is not exported by "src/main.ts" 排查方案

我的Vite项目(版本v4.5.0)执行npm run build时触发错误,提示“"default" is not exported by "src/main.ts"”,但执行开发服务npm run dev完全正常。以下是项目的配置文件,求帮忙定位问题:

vite.config.js

// Plugins
import vue from '@vitejs/plugin-vue'
import vuetify, { transformAssetUrls } from 'vite-plugin-vuetify'
import ViteFonts from 'unplugin-fonts/vite'

// Utilities
import { defineConfig } from 'vite'
import { fileURLToPath, URL } from 'node:url'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue({
      template: { transformAssetUrls }
    }),
    // https://github.com/vuetifyjs/vuetify-loader/tree/next/packages/vite-plugin
    vuetify({
      autoImport: true,
    }),
    ViteFonts({
      google: {
        families: [{
          name: 'Roboto',
          styles: 'wght@100;300;400;500;700;900',
        }],
      },
    }),
  ],
  define: { 'process.env': {} },
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    extensions: [
      '.js',
      '.json',
      '.jsx',
      '.mjs',
      '.ts',
      '.tsx',
      '.vue',
    ],
  },
  server: {
    port: 3535,
    host: true
  }
})

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "ESNext",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "moduleResolution": "Node10",
    "strict": true,
    "jsx": "preserve",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "lib": ["ESNext", "DOM"],
    "skipLibCheck": true,
    "noEmit": true,
    "allowJs": true,
    "noImplicitAny": false,
    "strictNullChecks": false,
    "paths": {
      "@/*": [
       "src/*"
      ]
     }
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue", "src/**/*.js"],
  "references": [{ "path": "./tsconfig.node.json" }],
  "exclude": ["node_modules"],
}

tsconfig.node.json

{
  "compilerOptions": {
    "composite": true,
    "module": "ESNext",
    "moduleResolution": "Node10",
    "allowSyntheticDefaultImports": true
  },
  "include": ["vite.config.ts"]
}

排查方向及解决方案

1. 检查src/main.ts的导出规范

构建阶段Rollup会严格校验ES模块的导出规则,而开发环境Vite的模块加载策略更宽松。如果main.ts只执行了挂载逻辑但没有导出任何内容,就会触发这个错误。

示例正确写法:

import { createApp } from 'vue'
import App from './App.vue'
import './assets/main.css'

const app = createApp(App)
app.mount('#app')

// 必须添加默认导出
export default app

2. 调整TypeScript的isolatedModules配置

你的tsconfig.json中isolatedModules设为true,这个选项要求每个TS文件都必须是合法模块(有明确的导入/导出)。如果不想给main.ts加导出,可以尝试将isolatedModules改为false,但这可能影响其他TS代码的校验规范,更推荐第一种方案。

3. 排查自定义入口配置

如果你的vite.config.js中自定义了build.rollupOptions.input,要确认指定的入口文件是否正确导出了默认模块。没有自定义的话可以忽略这一步。

4. 排查插件兼容性

临时移除vite-plugin-vuetify和unplugin-fonts插件,重新执行构建。如果报错消失,说明是插件版本与Vite v4.5.0不兼容,尝试升级或降级插件版本。


内容的提问来源于stack exchange,提问作者Jonathan Couto

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.04 11:27:44