Path alias causes "cannot find module" ts(2307) error in one .vue file but not another .vue file?

Viewed 2260

I'm facing a strange issue while trying to use path aliases (~/utils/assets) in my Vue 3 SFC (single file component) file. I am using the Volar VS Code plugin, and the issue occurs even after enabling "Takeover mode" with the Volar plugin.

  1. As you can see in the screenshot below, the Home.vue file is able to reference import { zipFromImages } from "~/utils/assets"; without any issues. However the Preview.vue file—which is located in the same directory as Home.vue—is NOT, and throws the ts(2307) error: Cannot find module '~/utils/assets' or its corresponding type declarations.ts(2307).
  2. When I select the Preview.vue file (a Vue SFC .vue file), the bottom of VS Code shows "No tsconfig". When I select Home.vue file, it properly shows "tsconfig.json" there and links to my tsconfig.json file as expected.

VS Code errors

Has anyone else faced these issues and found a solution?

What I've tried

Configuration

tsconfig.json

{
    "compilerOptions": {
        "target": "es6",
        "lib": [
            "es6",
            "dom"
        ],
        "outDir": "./dist",
        "module": "esnext",
        "isolatedModules": true,
        "strict": true,
        "skipLibCheck": true,
        "esModuleInterop": true,
        "moduleResolution": "node",
        "noUnusedLocals": true,
        "noImplicitAny": true,
        "allowJs": true,
        "resolveJsonModule": true,
        "baseUrl": "./",
        "paths": {
            "@/*": [
                "./src/ui/*"
            ],
            "~/*": [
                "./src/ui/*"
            ]
        },
        "typeRoots": [
            "./node_modules/@types",
            "./node_modules/@figma"
        ]
    },
    "include": [
        "src/**/*.ts",
        "src/**/*.d.ts",
        "src/**/*.tsx",
        "src/**/*.vue"
    ],
    "exclude": [
        "node_modules",
        "dist"
    ]
}

vite.config.ts

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import { viteSingleFile } from "vite-plugin-singlefile";
import path from "path";

export default defineConfig(({ mode }) => {
  const isDev = mode === "development";
  return {
    plugins: [vue(), viteSingleFile()],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src/ui"),
        "~": path.resolve(__dirname, "src/ui"),
      },
    },
    build: {
      outDir: path.resolve(__dirname, "dist/ui"), // The output directory
      minify: !isDev,
      sourcemap: true,
      watch: isDev
        ? {
            exclude: ["dist", "node_modules"],
          }
        : null,
      target: "esnext",
      assetsInlineLimit: 100000000,
      chunkSizeWarningLimit: 100000000,
      cssCodeSplit: false,
      brotliSize: false,
      rollupOptions: {
        output: {
          manualChunks: () => "everything.js",
        },
        inlineDynamicImports: true,
      },
    },
  };
});

Version information

VS Code & VS Code plugins

  • VS Code 1.65.2
  • Vue Language Features (Volar) v0.33.2
  • TypeScript Vue Plugin (Volar) v0.33.2

NPM Packages

  • Vite 2.8.6
  • Vue 3.2.6
  • Typescript 4.6.2
  • @vue/compiler-sfc 3.2.6
  • @vitejs/plugin-vue 2.2.4
  • vite-plugin-singlefile 0.7.1
0 Answers
Related