

How to avoid using relative path imports in Next.js
source link: https://whitep4nth3r.com/blog/how-to-avoid-using-relative-path-imports-next-js
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to avoid using relative path imports in Next.js
Does this look familiar? 🤯
import MyComponent from "../../../../../components/MyComponent";
import ADifferentFile from "../../../some/other/dir/ADifferentFile";
Relative import paths to files in any application can be tricky to manage. Often we rely on the intelligence of our IDEs to tell us how many dot-dot-slashes to type when we're importing files that are nested many directories deep. If you're working with Next.js — there's a better way!
Define your base directories — or module aliases — in a jsconfig.json file at the root of your Next.js project.
Here's the jsconfig.json file I use for the code that powers whitep4nth3r.com.
{
"compilerOptions": {
"baseUrl": "./",
"paths": {
"@components/*": ["components/*"],
"@contentful/*": ["contentful/*"],
"@layouts/*": ["layouts/*"],
"@styles/*": ["styles/*"],
"@utils/*": ["utils/*"]
}
}
}
Using module aliases, import paths at the top of files are self-documenting and easier to write, meaning you can focus on writing code rather than traversing spaghetti directories. It's beautiful.
import PageMeta from "@components/PageMeta";
import RecentPostList from "@components/RecentPostList";
import SocialCards from "@components/SocialCards";
import ContentfulBlogPost from "@contentful/BlogPost";
import MainLayout from "@layouts/main";
import Styles from "@styles/BaseStyles.module.css";
import { Config } from "@utils/Config";
Read more about absolute imports and module path aliases on the Next.js documentation.
whitep4nth3r | Salma Alam-Naylor
I help developers build stuff, learn things, and love what they do • I code live on Twitch • DevRel • She/Her
Recommend
-
15
Organising Imports With Typescript Path Mapping. I read about this cool feature a while back, as I didn’t like t...
-
8
Mar 9, 2017 Office 365: Office 2016 Installation from USBKey using relative path [30/04/17] Update: Following this post, I’ve pushed this script to
-
5
C # Relative path advertisements Hello I have a program made in Visual Studio in C#. In this program I have some pictures that need to be call...
-
9
Get the image of the relative path advertisements In my project I have 2 packages. images - contain images and notifi...
-
7
Copy link Contributor inquisitivecrystal
-
5
Copy link Collaborator
-
8
Clean Code19 April, 2022Clean Code Tip: Avoid using too many Imports in your classes
-
19
Python relative imports in AWS Lambda fail with attempted relative import with no known parent package The Problem In AWS Lambda if I attempt an explicit relative import like this . ├── lambda_f...
-
4
As a follow-up to Automatic width and height of local MDX images with Next.js, here's another post about how I resolved an issue when transitioning f...
-
8
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK