Absolute Imports and Module Path Aliases
Examples
Next.js has in-built support for the "paths"
and "baseUrl"
options of tsconfig.json
and jsconfig.json
files.
These options allow you to alias project directories to absolute paths, making it easier to import modules. For example:
Good to know:
create-next-app
will prompt to configure these options for you.
Absolute Imports
The baseUrl
configuration option allows you to import directly from the root of the project.
An example of this configuration:
Module Aliases
In addition to configuring the baseUrl
path, you can use the "paths"
option to "alias" module paths.
For example, the following configuration maps @/components/*
to components/*
:
Each of the "paths"
are relative to the baseUrl
location. For example: