Absolute Imports: How to Clean Up Your Project?
Every developer has encountered this:
import Button from '../../../../components/ui/Button';
The deeper the nesting — the scarier it gets.
A clean structure without staircase imports like ../../../../../
.
import Button from 'components/ui/Button';
You change the structure — and nothing breaks.
No more counting how many levels up you need to go.
All imports look the same, no matter how deep the file is.
Modern IDEs (WebStorm, VSCode) understand absolute paths and offer:
vite.config.ts
:
import { defineConfig } from 'vite';
import path from 'path';
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, './src')
}
}
});
tsconfig.json
:
{
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@/*": ["*"]
}
}
}
webpack.config.js
:
const path = require('path');
module.exports = {
resolve: {
alias: {
'@': path.resolve(__dirname, 'src')
}
}
};
In jsconfig.json
or tsconfig.json
:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@components/*": ["components/*"],
"@utils/*": ["utils/*"]
}
}
}
Use like this:
import Button from '@components/ui/Button';
Absolute imports are like tidying up your project:
It feels strange at first, then you wonder how you ever lived without it.
📌 Set up aliases, add them to tsconfig
, and forget about ../../../
.
Your code and your team will thank you 🙌