Vite 构建报错:路径包含中文导致文件找不到

错误提示中包含了 URL 编码的中文字符(如 %E4%BC%9A%E5%91%98%E8%B4%AD),说明项目路径中存在中文字符。Vite 或 Node.js 在处理路径时未能正确解码这些字符,导致文件加载失败。


🔍 原因分析

  • 使用了 new URL('.', import.meta.url).pathname 获取当前路径;
  • 此方法返回的是 URL 编码后的路径字符串
  • 如果路径中包含中文字符,会被编码为 %E4%... 这类形式;
  • Vite 内部无法自动解码这些路径,导致文件读取失败。

✅ 解决方案

✅ 方法一:改用 process.cwd() + path.join()

推荐方式,稳定且兼容性好:

import { join } from 'path'
import { defineConfig } from 'vite'

const rootPath = process.cwd()

export default defineConfig({
    resolve: {
        alias: {
            '@': join(rootPath, 'src'),
            stores: join(rootPath, 'src', 'stores'),
            wailsjs: join(rootPath, 'wailsjs'),
        },
    },
    // 其他配置不变...
})
posted @ 2025-07-08 20:22  灵火  阅读(75)  评论(0)    收藏  举报