I’m encountering the following error while setting up Swagger in my NestJS project:
/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:189
let schemaObjectName = trueMetadataType.name;
^
TypeError: Cannot read properties of null (reading 'name')
at SchemaObjectFactory.createNotBuiltInTypeReference (/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:189:49)
at SchemaObjectFactory.createSchemaMetadata (/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:297:25)
at SchemaObjectFactory.mergePropertyWithMetadata (/app/node_modules/@nestjs/swagger/dist/services/schema-object-factory.js:137:21)
...
This error occurs when generating the Swagger document in my main.ts
file:
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(
new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
}),
);
app.enableCors();
const options = new DocumentBuilder()
.setTitle('My API')
.setDescription('My API description')
.setVersion('1.0')
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, options); // error here
SwaggerModule.setup('swagger', app, document);
const configService = app.get(ConfigService);
const port = configService.get<number>('port');
await app.listen(port || 8000);
}
I’m seeking advice on the cause of this error and how to resolve it. Specifically, I’d like to know:
- What are the common causes of this error?
- Are there any specific areas I should check in my DTO or entity classes?
- Are there any precautions I should take when using Swagger decorators?
- Can you suggest a step-by-step approach to troubleshoot and resolve this issue?
Versions I’m using:
- NestJS: ^10.0.0
- @nestjs/swagger: ^7.4.2
Thank you for your help!