alias-solver
v1.0.1
Published
Convert relative imports to alias-based imports automatically — and back. Auto-detects tsconfig, vite, and webpack alias configs.
Maintainers
Readme
alias-solver
A CLI tool that automatically converts relative imports (../../foo) to alias-based imports (@/foo) — and back. Works out of the box: auto-detects existing alias configs, or generates them from your folder structure if none exist.
Features
- Zero-config: no aliases configured? The tool scans your
src/folders and generates them for you - Auto-detection of existing alias config from tsconfig / vite / webpack
- Converts relative imports → alias imports
- Reverts alias imports → relative imports (
--revert) - Dry-run mode to preview changes before applying
- Targets a specific file or directory with
--path - Handles
import,require(), dynamicimport(), andexport ... fromstatements - Picks the most specific alias (e.g.
@ui/over@components/over@/) - Colorized terminal output showing every change
- Fully idempotent — running twice produces no extra changes
Quick start
No install needed — just run in your project:
npx alias-solverThat's it. If your project has no alias config, the tool will:
- Scan your
src/(orapp/,lib/) directory - Generate aliases based on folder names (
@components/*,@utils/*, etc.) - Write them to
tsconfig.json - Convert all relative imports to alias imports
Installation
# Use directly (no install)
npx alias-solver
# Or install globally
npm install -g alias-solver
# Or install locally in your project
npm install --save-dev alias-solverUsage
alias-solver [options]
Options:
-d, --dry-run Preview changes without modifying files
-r, --revert Revert alias imports back to relative paths
-p, --path <p> Target a specific file or directory
-R, --recursive With --path, include subdirectories (default: direct files only)
-h, --help Show help
-v, --version Show versionConvert relative → alias
alias-solverPreview changes first
alias-solver --dry-runRevert alias → relative
alias-solver --revertTarget a specific folder (direct files only)
alias-solver --path src/hooks
# Only processes files directly in src/hooks/ (useAuth.ts, useTheme.ts)
# Does NOT touch files in subdirectoriesTarget a folder recursively
alias-solver --path src/components --recursive
# Processes all files in src/components/ AND all subdirectories
# (ui/buttons/Button.tsx, layout/sidebar/Sidebar.tsx, etc.)Target a single file
alias-solver --path src/pages/dashboard/Dashboard.tsxCombine flags
All flags work together. --revert respects --path and --recursive:
# Revert only direct files in src/hooks/
alias-solver --revert -p src/hooks
# Revert all files in src/components/ and subdirectories
alias-solver --revert -p src/components -R
# Preview a scoped revert without modifying anything
alias-solver --revert --dry-run -p src/components -RHow it works
With existing config
If your project already has aliases defined in tsconfig.json, vite.config, or webpack.config, the tool reads them and converts imports accordingly.
Without config (auto-generation)
If no aliases are found, the tool scans your source directory and generates them automatically:
src/
├── components/ → @components/*
├── hooks/ → @hooks/*
├── lib/ → @lib/*
├── pages/ → @pages/*
├── services/ → @services/*
├── store/ → @store/*
├── types/ → (via catch-all @/*)
└── utils/ → @utils/*
Plus catch-all: @/* → src/*The generated aliases are written to tsconfig.json automatically, then imports are converted.
Note: Folder names that conflict with npm scopes (like
types) are skipped as dedicated aliases and handled by the catch-all@/*instead (e.g.@/types).
Example
Before:
import { Sidebar } from '../../components/layout/sidebar/Sidebar';
import { BarChart } from './widgets/charts/BarChart';
import { useAuth } from '../../hooks/useAuth';
import { User } from '../../types';After:
import { Sidebar } from '@components/layout/sidebar/Sidebar';
import { BarChart } from '@pages/dashboard/widgets/charts/BarChart';
import { useAuth } from '@hooks/useAuth';
import { User } from '@/types';Supported alias configurations
tsconfig.json (recommended)
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@/*": ["src/*"],
"@components/*": ["src/components/*"],
"@utils/*": ["src/utils/*"]
}
}
}vite.config.ts
export default defineConfig({
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
'@components': path.resolve(__dirname, 'src/components'),
},
},
});webpack.config.js
module.exports = {
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
'@components': path.resolve(__dirname, 'src/components'),
},
},
};Detection priority: tsconfig.json > vite.config.{ts,js} > webpack.config.{js,ts} > auto-generation from folders.
Warning: Avoid naming an alias
@types/*— it conflicts with npm's@typesscope (used for DefinitelyTyped packages like@types/node). Use the catch-all@/*instead, which produces@/types.
Man page
After installing globally, you can view the manual:
man alias-solverLicense
MIT
