Skip to main content
Path Mapping allows you to import modules using absolute paths instead of relative paths. This improves code readability and eliminates the need to update import paths when files are moved.

Path Mapping in Web (React)

Sonamu Web projects provide @/* path mapping by default.

tsconfig.json Configuration

web/tsconfig.json

Vite Configuration

TypeScript configuration alone is not enough; you also need to set up the same alias in Vite.
web/vite.config.ts
You must configure both TypeScript and Vite. If only one is configured, type checking or builds will fail.

Usage Examples

src/pages/users/UserDetail.tsx
Problems:
  • Need to update all imports when file location changes
  • Complex paths like ../../../
  • Poor readability

Directory-specific Mapping

You can map more paths.
web/tsconfig.json
web/vite.config.ts
Usage examples:
We recommend using only @/*. Too many mappings can cause confusion.

Module Resolution in API Server

The API server typically does not use path mapping.

Reason

api/tsconfig.json
Vite resolves all modules
  • API is built with Vite, so separate path mapping is unnecessary
  • Packages like sonamu are automatically resolved by Vite
  • Relative paths are clearer and simpler

API Structure

API has a relatively simple structure.
api/src/application/controllers/UserController.ts
You can add path mapping to API if needed, but it’s generally unnecessary.

Importing Sonamu Packages

Sonamu itself can be imported without path mapping.
Sonamu package structure:
  • sonamu - Main package
  • sonamu/storage - Storage drivers
  • sonamu/cache - Cache drivers
Sonamu provides subpackages through the exports field in package.json.

Type Imports

Use the type keyword when importing only types.
type imports are removed after compilation, reducing bundle size.

IDE Auto-completion

When path mapping is configured, IDEs automatically recognize it.

VS Code

.vscode/settings.json
This setting uses absolute paths for auto imports.

IntelliJ / WebStorm

Automatically reads tsconfig.json and recognizes path mapping.
Symptoms:
  • @/... paths show red underline
  • Auto-completion doesn’t work
Solutions:
  1. Restart TypeScript server
  2. Check tsconfig.json
  3. Restart Vite dev server
  4. Reinstall node_modules

Jest Test Configuration

Jest configuration is needed to use path mapping in tests.
web/jest.config.ts
Usage in test files:
src/components/Button.test.tsx

Common Troubleshooting

Symptom:
Causes:
  1. baseUrl is not set
  2. Vite alias is not configured
  3. Path typo
Solutions:1. Check tsconfig.json
2. Check vite.config.ts
3. Verify file exists
Symptom:
Cause:
  • Vite recognizes the alias but TypeScript doesn’t
Solution:Add baseUrl and paths to tsconfig.json:
Symptom:
Cause:
  • TypeScript recognizes the alias but Vite doesn’t
Solution:Add alias to vite.config.ts:

Recommendations

Recommended settings:
Usage:
  • @/components/* - Components
  • @/pages/* - Pages
  • @/utils/* - Utilities
  • @/hooks/* - Custom hooks
  • @/types/* - Type definitions
Avoid:
  • Too many aliases (adds confusion)
  • Alias for every subdirectory (unnecessary)

Next Steps

tsconfig.json

Check the complete TypeScript configuration

Type Checking

Learn about type check options

Writing Components

Write React components

Project Structure

Check the overall project structure