Systematically debug NestJS issues including dependency injection errors, circular dependencies, module import problems, guard/interceptor failures, and microservice communication errors.
Ships in debug-and-refactor. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
This guide provides a systematic approach to debugging NestJS applications. Use the four-phase methodology below to efficiently identify and resolve issues.
Error Message:
Nest can't resolve dependencies of the <ProviderName> (?). Please make sure that the argument <DependencyName> at index [0] is available in the <ModuleName> context.
Common Causes:
providers array@Injectable() decorator on service classforRoot()/forRootAsync() configurationSolutions:
// Ensure provider is in the module
@Module({
providers: [MyService], // Add missing provider here
exports: [MyService], // Export if used by other modules
})
export class MyModule {}
// Ensure @Injectable() decorator exists
@Injectable()
export class MyService {
constructor(private readonly dependency: OtherService) {}
}
// For external modules, import the module not just the service
@Module({
imports: [OtherModule], // Import the module
})
export class MyModule {}
Error Message:
Nest cannot create the module instance. The module at index [X] of the imports array is undefined.
Common Causes:
Solutions:
// Use forwardRef() for circular module dependencies
@Module({
imports: [forwardRef(() => OtherModule)],
})
export class MyModule {}
// Use forwardRef() for circular service dependencies
@Injectable()
export class ServiceA {
constructor(
@Inject(forwardRef(() => ServiceB))
private serviceB: ServiceB,
) {}
}
// Alternative: Extract shared logic to a third module
@Module({
providers: [SharedService],
exports: [SharedService],
})
export class SharedModule {}
Error Message:
Cannot read property 'canActivate' of undefined
Cannot read property 'intercept' of undefined
Common Causes:
@UseGuards() or @UseInterceptors() decoratorSolutions:
// Register globally in main.ts
app.useGlobalGuards(new AuthGuard());
app.useGlobalInterceptors(new LoggingInterceptor());
// Or register via module for DI support
@Module({
providers: [
{
provide: APP_GUARD,
useClass: AuthGuard,
},
{
provide: APP_INTERCEPTOR,
useClass: LoggingInterceptor,
},
],
})
export class AppModule {}
// Controller-level registration
@UseGuards(AuthGuard)
@UseInterceptors(LoggingInterceptor)
@Controller('users')
export class UsersController {}
Error Message:
An instance of an invalid class-validator value was provided
Validation failed (expected type)
Common Causes:
class-transformer or class-validator packagesSolutions:
// Enable ValidationPipe globally with proper options
app.useGlobalPipes(
new ValidationPipe({
whitelist: true, // Strip non-whitelisted properties
forbidNonWhitelisted: true, // Throw on extra properties
transform: true, // Auto-transform payloads to DTO instances
transformOptions: {
enableImplicitConversion: true,
},
}),
);
// Properly decorate DTOs
import { IsString, IsInt, Min, IsOptional } from 'class-validator';
import { Type } from 'class-transformer';
export class CreateUserDto {
@IsString()
name: string;
@IsInt()
@Min(0)
@Type(() => Number)
age: number;
@IsOptional()
@IsString()
email?: string;
}
Error Message:
There is no matching message handler defined in the remote service
Connection refused / ECONNREFUSED
Common Causes:
Solutions:
// Ensure matching patterns
// Server side
@MessagePattern({ cmd: 'get_user' })
async getUser(data: { id: number }) {
return this.usersService.findOne(data.id);
}
// Client side - pattern must match exactly
const user = await this.client.send({ cmd: 'get_user' }, { id: 1 }).toPromise();
// Check transport configuration matches
// Server
const app = await NestFactory.createMicroservice<MicroserviceOptions>(
AppModule,
{
transport: Transport.TCP,
options: { host: '0.0.0.0', port: 3001 },
},
);
// Client module
ClientsModule.register([
{
name: 'USER_SERVICE',
transport: Transport.TCP,
options: { host: 'localhost', port: 3001 },
},
]);
Error Message:
Gateway is not defined
WebSocket connection failed
Common Causes:
@WebSocketGateway() decoratorSolutions:
// Properly configure gateway
@WebSocketGateway({
cors: {
origin: '*',
},
namespace: '/events',
})
export class EventsGateway implements OnGatewayConnection {
@WebSocketServer()
server: Server;
handleConnection(client: Socket) {
console.log('Client connected:', client.id);
}
@SubscribeMessage('message')
handleMessage(client: Socket, payload: any): string {
return 'Hello world!';
}
}
// Add to module
@Module({
providers: [EventsGateway],
})
export class EventsModule {}
// Configure adapter in main.ts if needed
import { IoAdapter } from '@nestjs/platform-socket.io';
app.useWebSocketAdapter(new IoAdapter(app));
# Start with debug flag
nest start --debug --watch
# Or add to package.json
"start:debug": "nest start --debug --watch"
Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug NestJS",
"runtimeExecutable": "npm",
"runtimeArgs": ["run", "start:debug"],
"console": "integratedTerminal",
"restart": true,
"autoAttachChildProcesses": true,
"sourceMaps": true,
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "attach",
"name": "Attach to NestJS",
"port": 9229,
"restart": true,
"sourceMaps": true
}
]
}
# docker-compose.debug.yml
version: '3.8'
services:
api:
command: npm run start:debug
ports:
- "3000:3000"
- "9229:9229" # Debug port
environment:
- NODE_OPTIONS=--inspect=0.0.0.0:9229
import { Logger, Injectable } from '@nestjs/common';
@Injectable()
export class MyService {
private readonly logger = new Logger(MyService.name);
async doSomething() {
this.logger.log('Processing started');
this.logger.debug('Debug info', { data: someData });
this.logger.warn('Warning message');
this.logger.error('Error occurred', error.stack);
this.logger.verbose('Verbose details');
}
}
// Configure logger level in main.ts
const app = await NestFactory.create(AppModule, {
logger: ['error', 'warn', 'log', 'debug', 'verbose'],
});
import { Test, TestingModule } from '@nestjs/testing';
import { INestApplication } from '@nestjs/common';
import * as request from 'supertest';
describe('UsersController (e2e)', () => {
let app: INestApplication;
beforeEach(async () => {
const moduleFixture: TestingModule = await Test.createTestingModule({
imports: [AppModule],
})
.overrideProvider(UsersService)
.useValue(mockUsersService)
.compile();
app = moduleFixture.createNestApplication();
await app.init();
});
it('/users (GET)', () => {
return request(app.getHttpServer())
.get('/users')
.expect(200)
.expect({ data: [] });
});
});
Before debugging, collect all relevant information:
# Check NestJS and dependency versions
npm list @nestjs/core @nestjs/common @nestjs/platform-express
# Check for peer dependency issues
npm ls 2>&1 | grep -i "peer dep"
# Review recent changes
git diff HEAD~5 --name-only | grep -E '\.(ts|json)$'
# Check environment configuration
cat .env | grep -v -E '^#|^$'
Key Questions:
Narrow down the issue systematically:
// 1. Check module structure
import { Logger } from '@nestjs/common';
@Module({
imports: [
// Comment out imports one by one to isolate
ConfigModule.forRoot(),
// DatabaseModule, // Try disabling
// UsersModule, // Try disabling
],
})
export class AppModule {
constructor() {
Logger.log('AppModule initialized');
}
}
// 2. Add lifecycle logging
@Injectable()
export class MyService implements OnModuleInit, OnModuleDestroy {
private readonly logger = new Logger(MyService.name);
onModuleInit() {
this.logger.log('MyService initialized');
}
onModuleDestroy() {
this.logger.log('MyService destroyed');
}
}
// 3. Test dependency injection manually
@Controller()
export class TestController {
constructor(
@Optional() private myService?: MyService,
) {
console.log('MyService injected:', !!this.myService);
}
}
Apply targeted debugging techniques:
// 1. For DI issues - check the module graph
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule, {
logger: ['verbose'], // Enable all logging
});
// Log all registered routes
const server = app.getHttpServer();
const router = server._events.request._router;
console.log('Registered routes:', router.stack
.filter(r => r.route)
.map(r => `${Object.keys(r.route.methods)} ${r.route.path}`));
await app.listen(3000);
}
// 2. For async configuration issues
@Module({
imports: [
ConfigModule.forRootAsync({
useFactory: async () => {
console.log('ConfigModule factory called');
const config = await loadConfig();
console.log('Config loaded:', config);
return config;
},
}),
],
})
export class AppModule {}
// 3. For guard/interceptor debugging
@Injectable()
export class DebugGuard implements CanActivate {
private readonly logger = new Logger(DebugGuard.name);
canActivate(context: ExecutionContext): boolean {
const request = context.switchToHttp().getRequest();
this.logger.debug(`Guard checking: ${request.method} ${request.url}`);
this.logger.debug(`Headers: ${JSON.stringify(request.headers)}`);
this.logger.debug(`User: ${JSON.stringify(request.user)}`);
return true; // Allow through for debugging
}
}
Ensure the fix is complete and documented:
// 1. Write a test for the fixed issue
describe('UserService', () => {
it('should resolve dependencies correctly', async () => {
const module = await Test.createTestingModule({
imports: [UsersModule],
}).compile();
const service = module.get<UsersService>(UsersService);
expect(service).toBeDefined();
expect(service.userRepository).toBeDefined();
});
});
// 2. Add error handling to prevent recurrence
@Injectable()
export class RobustService {
private readonly logger = new Logger(RobustService.name);
async riskyOperation() {
try {
return await this.externalCall();
} catch (error) {
this.logger.error(`Operation failed: ${error.message}`, error.stack);
throw new InternalServerErrorException('Operation failed');
}
}
}
// 3. Document in comments
/**
* UserService handles user-related operations.
*
* IMPORTANT: This service depends on DatabaseModule being imported
* before UsersModule in AppModule to avoid circular dependency issues.
*
* @see https://docs.nestjs.com/fundamentals/circular-dependency
*/
@Injectable()
export class UsersService {}
# Start in debug mode
npm run start:debug
# Start with increased heap memory
NODE_OPTIONS="--max-old-space-size=4096" npm run start:debug
# Run with verbose logging
LOG_LEVEL=verbose npm run start
# Debug specific module loading
DEBUG=nest:* npm run start
# Check TypeScript compilation
npx tsc --noEmit
# Validate module structure
npx nest info
# Clear node_modules and reinstall
rm -rf node_modules package-lock.json
npm install
# Rebuild NestJS
npm run build
rm -rf dist && npm run build
# Clear cache
npm cache clean --force
# Check for duplicate packages
npm dedupe
# Update NestJS packages
npx npm-check-updates -u "@nestjs/*"
npm install
# List all modules and providers
npx nest info
# Check circular dependencies
npx madge --circular --extensions ts src/
# Analyze bundle size
npx source-map-explorer dist/main.js
# Check TypeScript config
npx tsc --showConfig
# Verify imports
npx ts-unused-exports tsconfig.json
# Access container shell
docker exec -it <container_id> sh
# View container logs
docker logs -f <container_id>
# Check container environment
docker exec <container_id> env
# Debug with docker compose
docker compose -f docker-compose.debug.yml up
# Attach to running container
docker attach <container_id>
import {
ExceptionFilter,
Catch,
ArgumentsHost,
HttpException,
HttpStatus,
Logger,
} from '@nestjs/common';
@Catch()
export class AllExceptionsFilter implements ExceptionFilter {
private readonly logger = new Logger(AllExceptionsFilter.name);
catch(exception: unknown, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse();
const request = ctx.getRequest();
const status =
exception instanceof HttpException
? exception.getStatus()
: HttpStatus.INTERNAL_SERVER_ERROR;
const message =
exception instanceof HttpException
? exception.getResponse()
: 'Internal server error';
const errorResponse = {
statusCode: status,
timestamp: new Date().toISOString(),
path: request.url,
method: request.method,
message: typeof message === 'string' ? message : (message as any).message,
};
this.logger.error(
`${request.method} ${request.url} ${status}`,
exception instanceof Error ? exception.stack : String(exception),
);
response.status(status).json(errorResponse);
}
}
// Register globally
app.useGlobalFilters(new AllExceptionsFilter());
// 1. Add timing interceptor
@Injectable()
export class TimingInterceptor implements NestInterceptor {
private readonly logger = new Logger(TimingInterceptor.name);
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const now = Date.now();
const request = context.switchToHttp().getRequest();
return next.handle().pipe(
tap(() => {
const elapsed = Date.now() - now;
this.logger.log(`${request.method} ${request.url} - ${elapsed}ms`);
if (elapsed > 1000) {
this.logger.warn(`Slow request: ${request.url} took ${elapsed}ms`);
}
}),
);
}
}
// 2. Profile database queries
import { Logger } from '@nestjs/common';
// In TypeORM configuration
{
logging: ['query', 'error', 'warn'],
logger: 'advanced-console',
maxQueryExecutionTime: 1000, // Log slow queries
}
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
/debug-and-refactor:debug:nestjsfrom debug-and-refactor
npx claudepluginhub snakeo/claude-debug-and-refactor-skills-plugin --plugin debug-and-refactor45 skills in this plugin
/debug:angularDebug Angular applications systematically with expert-level diagnostic techniques. This skill provides comprehensive guidance for troubleshooting dependency injection errors, change detection issues (NG0100), RxJS subscription leaks, lazy loading failures, zone.js problems, and common Angular runtime errors. Includes structured four-phase debugging methodology, Angular DevTools usage, console debugging utilities (ng.probe), and performance profiling strategies for modern Angular applications./debug:djangoDebug Django web applications with systematic diagnostic approaches. This skill covers troubleshooting Django-specific errors including TemplateDoesNotExist, ImproperlyConfigured, IntegrityError, migration conflicts, CSRF failures, N+1 query problems, and circular imports. Includes Django Debug Toolbar setup, ORM query logging, pdb/ipdb usage, shell_plus debugging, and comprehensive logging configuration. Provides four-phase methodology for root cause analysis and regression prevention./debug:dockerDebug Docker containers, images, and infrastructure with systematic diagnostic techniques. This skill provides comprehensive guidance for troubleshooting container exit codes, OOM kills, image build failures, networking issues, volume mount problems, and permission errors. Covers four-phase debugging methodology from quick assessment to deep analysis, essential Docker commands, debug container techniques for minimal images, and platform-specific troubleshooting for Windows, Mac, and Linux.Other plugins' skills for the same job