DEV Community

Akilesh
Akilesh

Posted on • Updated on

Absolute Imports: Leveraging '@' for Efficient Direct Imports

" This article is published using Lamento🍋

import Layout from "../components/layout";
import { fetcher } from '../lib/fetcher';
Enter fullscreen mode Exit fullscreen mode

Add this configuration in your tsconfig.json

    "baseUrl": ".",
    "paths": {
      "@/components/*": ["components/*"],
      "@/lib/*": ["lib/*"],
      "@/hooks/*": ["lib/hooks/*"]
    },
Enter fullscreen mode Exit fullscreen mode

Image description

import Button from '@components/Layout'
import { fetcher } from '@lib/fetcher';
Enter fullscreen mode Exit fullscreen mode

make sure to restart the application in your localhost
Reference:
https://www.netlify.com/blog/2020/12/07/absolute-imports-in-next.js/

Top comments (2)

Collapse
 
framemuse profile image
Valery Zinchenko

I really like this approach, I adopted it almost a year ago and I really enjoy it.

Collapse
 
akilesh profile image
Akilesh

Thanks Val ✨ I think so everyone came across this method but we just don't know what it's called .