Skip to content

Instantly share code, notes, and snippets.

@jrc03c
Created September 2, 2022 15:39
Show Gist options
  • Save jrc03c/149f49af44cf102f31c266ab1b1cbb22 to your computer and use it in GitHub Desktop.
Save jrc03c/149f49af44cf102f31c266ab1b1cbb22 to your computer and use it in GitHub Desktop.
Create a module resolution alias in (e.g.) Vite
// Note that this uses the __dirname definition from here:
// https://gist.github.com/jrc03c/6fbccd5af3a4d1b61f12e1e620e1c01b
import { defineConfig } from "vite"
import path from "path"
const __dirname = path.resolve(
import.meta.url.replace("file://", "").split("/").slice(0, -1).join("/")
)
export default defineConfig({
resolve: {
alias: {
"@": __dirname,
},
},
})
// Now it's possible to import relative to the project root, like:
// import whatever from "@/path/to/whatever"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment