Bỏ qua

TypeScript Compilation Fix Guide

Problem Detected

TypeScript compiler reports files "not found" even though they exist on disk. This is typically a caching issue or tsconfig problem.

Quick Fix Steps

Step 1: Stop All Running Processes

# Kill any running dev servers
pkill -f "node.*field-service" || true
pkill -f "ts-node" || true

Step 2: Clear All Caches

cd /Users/trannhatsang/Documents/UIT_School/Semester04/IE505_THESIS/projects/football-booking-backend-boilerplate

# Clear TypeScript cache
rm -rf dist/
rm -rf node_modules/.cache/
rm -rf apps/field-service/dist/
rm -rf .nx/cache/

# Clear tsconfig build info
find . -name "*.tsbuildinfo" -delete

echo "✅ All caches cleared"

Step 3: Restart TypeScript Server in IDE

In VS Code / Cursor:

  1. Press Cmd+Shift+P (Mac) or Ctrl+Shift+P (Windows)
  2. Type: "TypeScript: Restart TS Server"
  3. Press Enter

Step 4: Rebuild from Scratch

# Install dependencies (if needed)
yarn install

# Build the project
yarn build

# Or start dev server
yarn dev

If Still Not Working

Fix 1: Check TypeScript Version

yarn why typescript
npx tsc --version

Should be 4.x or 5.x.

Fix 2: Verify Files Exist

# Check auth files
ls -la apps/field-service/src/auth/
ls -la apps/field-service/src/auth/decorators/
ls -la apps/field-service/src/auth/guards/
ls -la apps/field-service/src/auth/strategies/

# Check DTO files
ls -la apps/field-service/src/fields/dto/

# Should see ALL files listed without errors

Fix 3: Test Single File Compilation

# Test compile one file
npx tsc --noEmit apps/field-service/src/auth/auth.module.ts

# Should compile without errors

Fix 4: Update tsconfig.json (If Needed)

Edit apps/field-service/tsconfig.json:

{
  "extends": "../../tsconfig.base.json",
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "target": "ES2021",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": ".",
    "incremental": true,
    "skipLibCheck": true
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}

Common Errors & Solutions

Error: "Cannot find name 'super'"

File: auth/guards/keycloak-auth.guard.ts

Solution: Already fixed - changed return type to any instead of ReturnType<typeof super.canActivate>

Error: "File not found"

Cause: TypeScript cache or incorrect tsconfig

Solution:

  1. Clear cache (Step 2 above)
  2. Restart TS server (Step 3 above)
  3. Rebuild (Step 4 above)

Error: "Cannot find module './operating-hours.dto'"

File: dto/create-field.dto.ts

Solution: Not an issue - the file defines classes internally, doesn't import from external file

Verification Checklist

After following the steps above:

  • [ ] All cache directories cleared
  • [ ] TypeScript server restarted
  • [ ] yarn dev runs without "file not found" errors
  • [ ] No red squiggles in IDE
  • [ ] Build completes successfully

Nuclear Option (Last Resort)

If nothing else works:

# Complete reset
rm -rf node_modules yarn.lock
rm -rf dist .nx
yarn install
yarn build

Files That Should Exist

All these files MUST exist (verified they do):

Auth Module (7 files):

apps/field-service/src/auth/auth.module.tsapps/field-service/src/auth/index.tsapps/field-service/src/auth/decorators/public.decorator.tsapps/field-service/src/auth/decorators/roles.decorator.tsapps/field-service/src/auth/guards/keycloak-auth.guard.tsapps/field-service/src/auth/guards/roles.guard.tsapps/field-service/src/auth/strategies/keycloak.strategy.ts

DTOs (8 files):

apps/field-service/src/fields/dto/create-field.dto.tsapps/field-service/src/fields/dto/operating-hours.dto.tsapps/field-service/src/fields/dto/paginated-result.dto.tsapps/field-service/src/fields/dto/reject-field.dto.tsapps/field-service/src/fields/dto/reorder-images.dto.tsapps/field-service/src/fields/dto/search-fields.dto.tsapps/field-service/src/fields/dto/update-field-status.dto.tsapps/field-service/src/fields/dto/update-field.dto.ts

Controllers (2 files):

apps/field-service/src/fields/controllers/fields.controller.tsapps/field-service/src/fields/controllers/admin-fields.controller.ts

Services (2 files):

apps/field-service/src/fields/services/fields.service.tsapps/field-service/src/fields/services/image.service.ts

Entities (2 files):

apps/field-service/src/fields/entities/field.entity.tsapps/field-service/src/fields/entities/field-image.entity.ts

Guards (1 file):

apps/field-service/src/fields/guards/field-ownership.guard.ts

Config (2 files):

apps/field-service/src/config/storage.config.tsapps/field-service/src/config/index.ts

Migrations (1 file):

apps/field-service/src/migrations/1737000000000-UpdateFieldSchema.ts

Total: 32 files - all exist on disk!

Debug Commands

# Count TypeScript files
find apps/field-service/src -name "*.ts" ! -name "*spec.ts" | wc -l
# Should show: 35

# Check for any permission issues
find apps/field-service/src -name "*.ts" ! -perm -r
# Should show: (empty)

# Verify no duplicate files
find apps/field-service/src -name "*.ts" | sort | uniq -d
# Should show: (empty)

Expected Result

After completing these steps:

yarn dev
# Output should be:
# ✓ Compiled successfully
# Field Service running on: http://localhost:3002

No "file not found" errors! ✅