An alias in vite.config.ts results in "error TS2307: Cannot find module", but it works. #8240
-
An alias in vite.config.ts results in "error TS2307: Cannot find module", but it works. The first step was to create a vanilla TypeScript Vue3 project by executing
And I have set an alias as follows. vite.config.ts:
src/configs/development.ts | staging.ts | production.ts:
src/App.vue:
Files:
It works as I expected. But TypeScript says
|
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
For type check, you need to set |
Beta Was this translation helpful? Give feedback.
-
"declare" statement is needed in env.d.ts file.
|
Beta Was this translation helpful? Give feedback.
-
For those getting to this page from Google (or whatever SE you use), the solution is: import { fileURLToPath, URL } from "url";
import { defineConfig } from "vite";
// https://vitejs.dev/config/
export default defineConfig({
base: "./",
resolve: {
alias: {
"~": fileURLToPath(new URL("./src", import.meta.url)),
},
},
plugins: [/* ... */],
// ...
}); and your {
"compilerOptions": {
// ...
/* Path Aliases */
"baseUrl": ".",
"paths": {
"~/*": ["./src/*"]
}
},
// ...
} |
Beta Was this translation helpful? Give feedback.
-
For me the fix was to put it in compilerOptions of "types": [
"node"
],
"baseUrl": ".",
"paths": {
"~/*": [
"./src/*"
]
} |
Beta Was this translation helpful? Give feedback.
"declare" statement is needed in env.d.ts file.