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

Vue3+TypeScript项目报错:找不到模块声明文件

解决Vue3+TypeScript项目导入组件时的声明文件错误

1. 添加Vue组件类型声明文件

在项目根目录创建或检查是否存在shims-vue.d.ts文件,写入以下内容:

declare module '*.vue' {
  import type { DefineComponent } from 'vue'
  const component: DefineComponent<{}, {}, any>
  export default component
}

这个文件用于告诉TypeScript如何识别.vue后缀的模块,是解决此类报错的核心配置。

2. 校验tsconfig.json配置

打开项目根目录的tsconfig.json,确认以下关键配置:

  • include数组需覆盖源码目录和声明文件,确保TypeScript能扫描到相关文件:
    "include": [
      "src/**/*",
      "shims-vue.d.ts"
    ]
    
  • compilerOptions中moduleResolution设置为node,保证模块解析符合Node.js规则:
    "compilerOptions": {
      "moduleResolution": "node",
      // 其他原有配置...
    }
    

3. 规范组件的script标签

确保组件的<script>标签明确指定lang="ts",即使无业务逻辑也需声明类型语言:

<template>
  <div>
    <h1>Inventory</h1>
  </div>
</template>

<script setup lang="ts">
// 可在此添加TS相关代码
</script>

4. 重置依赖与缓存

若以上步骤无效,尝试清理依赖并重新安装:

rm -rf node_modules package-lock.json
npm install

同时清除TypeScript缓存:

npx tsc --clearCache

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 00:09:59