Advertisement
Intermediate Next.js, Next.js Project Setup & Configuration

How do you configure custom path aliases in a Next.js project so you can write cleaner import statements?

Short answer

You add a paths configuration inside your tsconfig.json or jsconfig.json file, mapping a short alias like @/components to your actual folder path, letting you write shorter, cleaner import statements that do not need long relative paths with many dots and slashes.

Save Next.js mock interview
Advertisement
AModel answer
You add a paths configuration inside your tsconfig.json or jsconfig.json file, mapping a short alias like @/components to your actual folder path, letting you write shorter, cleaner import statements that do not need long relative paths with many dots and slashes.
// tsconfig.json
{
  "compilerOptions": {
    "paths": {
      "@/*": ["./*"]
    }
  }
}

// Usage
import Button from '@/components/Button';
Real-world example
A large project sets up a clean @ alias pointing to its source folder, letting every file import shared components without long, hard to read relative paths like ../../../components.
Likely follow-ups
  • Does this configuration work the same way in both JavaScript and TypeScript projects?
  • What happens if two different aliases accidentally point to overlapping folders?
Related topics