Error on path aliases vite when trying to import ts file

Viewed 156

Error while importing ts file using path alias from vite, I have no issues if I import components (.vue) files but if I try to import ts I have this error

Error importing ts file using path alias from vite

Vite default config for alias

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

export default defineConfig({
    plugins: [vue()],
    resolve: {
        alias: {
            '@/': new URL('./src/', import.meta.url).pathname,
        },
    },
})
1 Answers

I had to add this

"paths": {
    "@/*": [
        "./src/*"
    ],
},

on my tsconfig.json file inside compilerOptions property. So, my tsconfig.json looks like this

{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "strict": true,
    "jsx": "preserve",
    "sourceMap": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "esModuleInterop": true,
    "lib": ["ESNext", "DOM"],
    "skipLibCheck": true,
    "paths": {
        "@/*": [
            "./src/*"
        ],
    },
  },
  "include": ["src/**/*.ts", "src/**/*.d.ts", "src/**/*.tsx", "src/**/*.vue"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

Related