Skip to content

Instantly share code, notes, and snippets.

@joeyjiron06
Last active June 13, 2024 21:22
Show Gist options
  • Save joeyjiron06/a7b8f214d6dcc3bbf4fbeb94c4e81d12 to your computer and use it in GitHub Desktop.
Save joeyjiron06/a7b8f214d6dcc3bbf4fbeb94c4e81d12 to your computer and use it in GitHub Desktop.
Setup ViteJS and Typescript to use import alias

Vite + Typescript import aliases

This is how you add import aliases in a vite project that uses Vite.

It requires you to update both your tsconfig file as well as your vite config so that vite knows how to import the aliases.

tsconfig.jcon

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "~/*": ["src/*"]
    }
  }
}

vite.config.ts

import { defineConfig } from 'vite'
import path from 'path'
import { fileURLToPath } from 'url'

const __dirname = path.dirname(fileURLToPath(import.meta.url))

export default defineConfig({
  // add this resolve config
  resolve: {
    alias: {
      '~': path.resolve(__dirname, './src'),
    },
  },
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment