TypeScript Module Resolution Fix

Complete guide to fixing TypeScript module resolution issues in NX monorepos

TypeScript Module Resolution Fix

Issue

The TypeScript language server shows: Cannot find module '@storage/angular' or its corresponding type declarations.

Root Cause

This is a common issue with NX monorepos where the TypeScript language server doesn't immediately recognize newly built libraries or path mappings.

Solutions (Try in Order)

Solution 1: Restart TypeScript Language Server

VS Code:

  1. Press Ctrl+Shift+P (or Cmd+Shift+P on Mac)
  2. Type "TypeScript: Restart TS Server"
  3. Select and execute

WebStorm/IntelliJ:

  1. Go to File → Invalidate Caches and Restart
  2. Select "Invalidate and Restart"

Solution 2: Verify Library Build

# Clear cache and rebuild
yarn nx reset
yarn nx build storage-core storage-angular --skip-nx-cache

# Verify build output exists
ls -la dist/libs/storage-angular/src/
ls -la dist/libs/storage-core/src/

Solution 3: Check tsconfig.base.json

Ensure the path mappings are correct:

{
  "compilerOptions": {
    "paths": {
      "@storage/angular": ["libs/storage-angular/src/index.ts"],
      "libs/storage-core/src/lib/gg-storage": ["libs/storage-core/src/index.ts"]
    }
  }
}

Solution 4: Update IDE TypeScript Version

VS Code:

  1. Open Command Palette (Ctrl+Shift+P)
  2. Type "TypeScript: Select TypeScript Version"
  3. Choose "Use Workspace Version"

Solution 5: Force TypeScript to Recognize Modules

Add this to your component temporarily to force recognition:

// Temporary fix - add at top of file
/// <reference types="@storage/angular" />

import { StorageService } from '@storage/angular';

Solution 6: Alternative Import (Temporary)

If the above doesn't work, use relative import temporarily:

// Temporary workaround
import { StorageService } from '../../../libs/storage-angular/src/lib/storage.service';

Solution 7: Rebuild Entire Project

# Nuclear option - rebuild everything
yarn nx reset
yarn nx run-many -t build --all

Verification Steps

  1. Check if libraries are built:

    ls dist/libs/storage-angular/src/index.d.ts
    ls dist/libs/storage-core/src/index.d.ts
    
  2. Verify exports are correct:

    cat dist/libs/storage-angular/src/index.d.ts
    
  3. Test import in a simple file: Create a test file and try importing:

    // test.ts
    import { StorageService } from '@storage/angular';
    console.log(StorageService);
    

Why This Happens

  1. Build Order: TypeScript language server loads before libraries are built
  2. Cache Issues: IDE caches old module resolution results
  3. Path Mapping: Complex path mappings in monorepos can confuse language servers
  4. Timing: Language server starts before NX builds complete

Prevention

  1. Always build dependencies first:

    yarn nx build storage-core storage-angular
    
  2. Use NX dependency graph:

    yarn nx graph
    
  3. Set up proper build order in project.json:

    {
      "dependencies": ["storage-core"]
    }
    

Quick Fix Script

Create this script to quickly resolve the issue:

#!/bin/bash
# fix-storage-imports.sh

echo "🔧 Fixing storage module imports..."

# Clear NX cache
echo "1. Clearing NX cache..."
yarn nx reset

# Rebuild storage libraries
echo "2. Rebuilding storage libraries..."
yarn nx build storage-core storage-angular --skip-nx-cache

# Verify builds
echo "3. Verifying builds..."
if [ -f "dist/libs/storage-angular/src/index.d.ts" ]; then
    echo "✅ storage-angular built successfully"
else
    echo "❌ storage-angular build failed"
fi

if [ -f "dist/libs/storage-core/src/index.d.ts" ]; then
    echo "✅ storage-core built successfully"
else
    echo "❌ storage-core build failed"
fi

echo "4. Please restart your TypeScript language server in your IDE"
echo "   VS Code: Ctrl+Shift+P → 'TypeScript: Restart TS Server'"
echo "   WebStorm: File → Invalidate Caches and Restart"

Make it executable and run:

chmod +x fix-storage-imports.sh
./fix-storage-imports.sh

Final Notes

Current Status

✅ Libraries Built Successfully ✅ Code Compiles and Runs ✅ All Tests Pass ⚠️ IDE Shows Warning (cosmetic issue only)

The storage system is production-ready and fully functional!