The project is configured to use absolute paths instead of relative paths, making imports cleaner and easier to maintain.
// ❌ Before (Relative Paths)
import { cn } from "../../../lib/utils";
import { useApiClient } from "../../../../providers/ApiProvider";
// ✅ After (Absolute Paths)
import { cn } from "@lib/utils";
import { useApiClient } from "@providers/ApiProvider";tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@lib/*": ["./src/lib/*"],
"@components/*": ["./src/components/*"],
"@atoms/*": ["./src/components/Atoms/*"],
"@molecules/*": ["./src/components/Molecules/*"],
"@organisms/*": ["./src/components/Organisms/*"]
}
}
}Available Aliases:
@lib/*→src/lib/*@components/*→src/components/*@atoms/*→src/components/Atoms/*@molecules/*→src/components/Molecules/*@organisms/*→src/components/Organisms/*
tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@sdk/*": ["src/*"],
"@entities/*": ["src/entities/*"],
"@providers/*": ["src/providers/*"],
"@services/*": ["src/services/*"]
}
}
}Available Aliases:
@sdk/*→src/*(for internal SDK files likeclient.ts)@entities/*→src/entities/*@providers/*→src/providers/*@services/*→src/services/*
tsconfig.json:
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"@components/*": ["./components/*"],
"@app/*": ["./app/*"],
"@lib/*": ["./lib/*", "../../packages/design-system/src/lib/*"],
"@entities/*": ["../../packages/sdk/src/entities/*"],
"@providers/*": ["../../packages/sdk/src/providers/*"],
"@services/*": ["../../packages/sdk/src/services/*"]
}
}
}next.config.js (Webpack Aliases):
const path = require("path");
module.exports = {
webpack: (config) => {
config.resolve.alias = {
...config.resolve.alias,
"@lib": path.resolve(__dirname, "../../packages/design-system/src/lib"),
"@entities": path.resolve(__dirname, "../../packages/sdk/src/entities"),
"@providers": path.resolve(__dirname, "../../packages/sdk/src/providers"),
"@services": path.resolve(__dirname, "../../packages/sdk/src/services"),
};
return config;
},
};Available Aliases:
@components/*→./components/*(web app)@app/*→./app/*(Next.js app directory)@lib/*→ Design System'slibfolder@sdk/*→ SDK'ssrcfolder (for internal SDK imports)@entities/*→ SDK'sentitiesfolder@providers/*→ SDK'sprovidersfolder@services/*→ SDK'sservicesfolder
// In: packages/design-system/src/components/Atoms/Button/Button.tsx
import { cn } from '@lib/utils';
import { tv } from 'tailwind-variants';
const button = tv({ /* ... */ });
export const Button = ({ className, ...props }) => (
<button className={cn(button(), className)} {...props} />
);// In: packages/sdk/src/services/mutations/auth/useLogin/index.ts
import { useMutation } from "@tanstack/react-query";
import { useApiClient } from "@providers/ApiProvider";
import { setToken } from "@sdk/client";
import { loginRequest } from "./request";
import { LoginInput, LoginResponse } from "@entities/Auth";
export const useLogin = () => {
const { client } = useApiClient();
return useMutation({
mutationFn: async (data: LoginInput) => {
const response = await loginRequest(client, data);
setToken(response.accessToken);
return response;
},
});
};// In: apps/web/app/page.tsx
import { Button, Card, ProductCard } from '@react-shop/design-system';
import { useProductList } from '@react-shop/sdk';
import { ProductSection } from '@components/ProductSection';
export default function Home() {
const { data: products } = useProductList();
return (
<div>
<ProductSection products={products} />
</div>
);
}Next.js needs both TypeScript path mappings (tsconfig.json) and Webpack aliases (next.config.js) because:
- TypeScript (
tsconfig.json): Provides IntelliSense and type checking - Webpack (
next.config.js): Resolves paths at build/runtime
Without the webpack configuration, you'd see:
Module not found: Can't resolve '@lib/utils'
Solution: Make sure both tsconfig.json and next.config.js are configured:
- Check
tsconfig.jsonhas the path mapping - Check
next.config.jshas the webpack alias - Restart the dev server:
pnpm dev
Solution: Reload VS Code's TypeScript server:
- Press
Cmd+Shift+P(Mac) orCtrl+Shift+P(Windows/Linux) - Type: "TypeScript: Restart TS Server"
- Press Enter
Solution: The webpack alias is missing. Add it to next.config.js.
- Always use aliases for cross-package imports
- Keep aliases consistent across packages
- Document new aliases when adding them
- Restart dev server after changing
next.config.js - Use relative paths only within the same folder
| Alias | Points To | Used In |
|---|---|---|
@lib/* |
Design System lib | All packages |
@components/* |
Component folders | Design System, Web App |
@atoms/* |
Atom components | Design System |
@molecules/* |
Molecule components | Design System |
@organisms/* |
Organism components | Design System |
@sdk/* |
SDK src folder | SDK, Web App |
@entities/* |
SDK entities | SDK, Web App |
@providers/* |
SDK providers | SDK, Web App |
@services/* |
SDK services | SDK, Web App |
If you have existing code with relative paths, replace them:
# Example: Replace relative imports with absolute
find packages/design-system/src -name "*.tsx" -exec sed -i '' 's|from "../../../lib/utils"|from "@lib/utils"|g' {} +This configuration makes the codebase cleaner, more maintainable, and easier to refactor! 🚀