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

如何解决Vite Vue项目中npm link本地包的Failed to resolve错误?

Vue3项目使用npm link本地包时的导入解析错误及临时解决方案

当Vue3项目通过npm link以符号链接方式引入本地包时,可能会遇到以下导入解析错误:

错误示例1:

12:29:14 PM [vite] Pre-transform error: Failed to resolve import "my-package/socket" from "src/main.ts". Does the file exist?

错误示例2:

Error: The following dependencies are imported but could not be resolved:

  my-package/socket (imported by 

已知该本地包的package.json中exports配置如下:

{
  "name": "my-package",
  "exports": {
    "./*": {
      "types": "./dist/*.d.ts",
      "default": "./dist/*.js"
    }
  }
}

临时解决方案

修改项目根目录下的vite.config.ts,添加别名配置:

// https://vitejs.dev/config/
export default defineConfig({
  // 其他原有配置
  resolve: {
    alias: {
      'my-package': fileURLToPath(new URL('node_modules/my-package/dist', import.meta.url)),
      // 其他别名配置
    }
  }
})

内容的提问来源于stack exchange,提问作者Yevhenii Kamenskyi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 07:22:06