Wouldn’t it be fantastic if we could eliminate the hassle of using lengthy relative paths in our imports for React components in Next.js? Well, guess what? We can achieve this convenience through the use of absolute imports.
Instead of writing imports like this:
import Layout from '../../components/layout'
We can simplify it to:
import Layout from 'components/layout'
So, how can we make this magic happen? It’s quite straightforward. All you need to do is create a file named jsconfig.json
and place it in the root directory of your project. Inside the file, include the following content:
{
"compilerOptions": {
"baseUrl": "."
}
}
That’s it! With this simple configuration, you can enjoy the benefits of absolute imports in Next.js. It truly simplifies your imports and makes your code more readable.
Tags: Next.js, React, absolute imports, import paths, configuration