Level Up Your Imports: Using Path Aliases for Cleaner React + TypeScript Projects πŸš€

@sagarkothari88 Β· 2025-07-31 04:31 Β· HiveDevs

Hello Hive Community Members! πŸ‘‹

Hope you're all doing fantastic! I'm back with Day 5 of my ReactJS learning series πŸš€
This journey has been quite the rollercoaster, and today we're diving into something every dev struggles with β€” those ugly relative imports 😩

But before that, if you missed my earlier episodes, here's your cheat sheet πŸ‘‡


πŸ“š Catch Up on My React Adventures


πŸš€ Why Path Aliases?

alias based import reference image

Let’s face it β€”

import Button from "../../../../components/ui/Button"

is just plain ugly and hard to maintain πŸ˜΅β€πŸ’«

While pairing up with another dev (shoutout to AI-generated code ✨), I noticed a cleaner import style β€” so I had to figure it out myself! Here’s how you can too πŸ‘‡


πŸͺ„ Step-by-Step: Setting Up Path Aliases

🧠 Step 1: Update tsconfig.app.json

Add these compiler options to set your base path and alias:

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["./src/*"]
    }
    // ...other stuff
  }
}

βš™οΈ Step 2: Tweak vite.config.ts

Use path module to create an alias for @ pointing to your src folder.

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";
import path from "path";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    react(), 
    tailwindcss()
  ],
  resolve: {
    alias: {
      "@": path.resolve(__dirname, "src"),
    },
  },
});

πŸŽ‰ Boom! You’re 90% done.


πŸ” Step 3: Restart Everything!

Restart Typescript server

  • Stop your npm run dev server.
  • Restart TypeScript in VS Code (click on the TS version and hit "Restart TS Server").
  • Before starting again... do not skip step 4 🀣

βœ‚οΈ Step 4: Update Your Imports!

Time to ditch the dot-dot-dot πŸ”ͺ

example updated import

Use beautiful imports like:

import Button from "@/components/ui/Button"

Isn’t that satisfying? 😍


πŸ“š Sources That Helped (But Also Confused Me πŸ˜‚)

Honestly... I followed these and ended up with TypeScript errors. So, the method described in this post is what actually worked for me! πŸ™Œ


Thanks for following along!
See you in the next post where I’ll probably break something else and then fix it (again) πŸ˜…

Stay curious & keep coding πŸ’»πŸ’ͺ



πŸ“ Final Note

  • I asked ChatGPT/AI to help optimize this post to make it more readable and viewer-friendly.
  • Here is the link where you can find both original content & improvements made by AI
  • https://chatgpt.com/share/68897d05-0eb0-8000-8f07-77280f4f2375

πŸš€ My Contributions to ♦️ Hive Ecosystem

Contribution To Hive Ecosystem
Hive Witness Node Hive API Node (in progress) 3Speak Video Encoder Node Operator (highest number of nodes) 3Speak Mobile App Developer
3Speak Podcast App Developer 3Speak Shorts App Developer 3Speak Support & Maintenance Team Distriator Developer
CheckinWithXYZ Hive Inbox HiFind Hive Donate App
Contributed to HiveAuth Mobile App Ecency ↔ 3Speak Integration Ecency ↔ InLeo Integration Ecency ↔ Actifit Integration
Hive Stats App Vote for Witness App HiveFlutterKit New 3Speak App

πŸ™Œ Support Back

❀️ Appreciate my work? Consider supporting @threespeak & @sagarkothari88! ❀️

Vote For Witness
sagarkothari88 @sagarkothari88
threespeak @threespeak

#hive #development #webapp #reactjs #javascript #frontend #aliases #india #waiv #neoxian
Payout: 0.000 HBD
Votes: 129
More interactions (upvote, reblog, reply) coming soon.