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
Usage Examples
- Before (Relative Paths)
- After (Absolute Paths)
src/pages/users/UserDetail.tsx
- 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
Module Resolution in API Server
The API server typically does not use path mapping.Reason
api/tsconfig.json
- API is built with Vite, so separate path mapping is unnecessary
- Packages like
sonamuare 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- Main packagesonamu/storage- Storage driverssonamu/cache- Cache drivers
Sonamu provides subpackages through the
exports field in package.json.Type Imports
Use thetype keyword when importing only types.
IDE Auto-completion
When path mapping is configured, IDEs automatically recognize it.VS Code
.vscode/settings.json
IntelliJ / WebStorm
Automatically readstsconfig.json and recognizes path mapping.
When auto-completion doesn't work
When auto-completion doesn't work
Symptoms:
@/...paths show red underline- Auto-completion doesnβt work
-
Restart TypeScript server
-
Check tsconfig.json
-
Restart Vite dev server
-
Reinstall node_modules
Jest Test Configuration
Jest configuration is needed to use path mapping in tests.web/jest.config.ts
src/components/Button.test.tsx
Common Troubleshooting
Cannot find module '@/...' error
Cannot find module '@/...' error
Symptom:Causes:2. Check vite.config.ts3. Verify file exists
baseUrlis not set- Vite alias is not configured
- Path typo
Build succeeds but type check fails
Build succeeds but type check fails
Symptom:Cause:
- Vite recognizes the alias but TypeScript doesnβt
baseUrl and paths to tsconfig.json:Type check succeeds but build fails
Type check succeeds but build fails
Symptom:Cause:
- TypeScript recognizes the alias but Vite doesnβt
vite.config.ts:Recommendations
- Web
- API
- Consistency
Recommended settings:Usage:
@/components/*- Components@/pages/*- Pages@/utils/*- Utilities@/hooks/*- Custom hooks@/types/*- Type definitions
- 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