Nine days after first writing this post, the Next.js team landed support for paths in tsconfig.json
and jsconfig.json
by default! In Next.js 9.4 and onwards, you only need to specify a baseURL
in your config file to support absolute imports:
To use a custom prefix, add a paths
configuration:
Editors like VSCode automatically support the config in jsconfig.json
, so Command+Click to jump to the source of a file will work as usual. Atom and IntelliJ also have support for rewrites.
The original post, using a babel plugin.
Relative import statements are a pain. To avoid ../
chains, improve code portability, and type less, I've started using babelplugin-module-resolver in my Next.js projects.
The goal is to transform verbose import statements like this:
into absolute import statements that work anywhere in your project:
Let's do it. Install the babel plugin as a devDependency
:
In the root of your Next.js project, create a .babelrc.json
file and add the module-resolver
plugin:
Create a jsconfig.json
(or tsconfig.json
if you're using TypeScript) and add the paths
property:
Note that the syntax is slightly different than the babel config.
If you're using a mixed JS/TS codebase, you should include JS files in your tsconfig.json
:
Now you can update your import statements to use the new syntax!