swagger: @nestjs/swagger and bson lib collision problem
I’m submitting a Bug report
[ ] Regression
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.
import { PrimaryKey, Property, SerializedPrimaryKey } from '@mikro-orm/core';
import { ObjectId } from 'bson';
export abstract class CommonEntity {
@PrimaryKey()
_id: ObjectId;
@SerializedPrimaryKey()
id!: string; // string variant of PK, will be handled automatically
@Property()
createdAt = new Date();
@Property({ onUpdate: () => new Date() })
updatedAt = new Date();
}
{
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"projects": {},
"compilerOptions": {
"webpack": false,
"plugins": [
{
"name": "@nestjs/swagger",
"options": {
"classValidatorShim": true,
"introspectComments": true
}
}
]
}
}
Current behavior
Error: Cannot find module ‘bson/bson’ Require stack:
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/entities/common.entity.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/modules/users/entities/user.entity.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/modules/users/users.service.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/modules/users/users.module.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/app.module.js
- /Volumes/HD/workspace/_temp/hello-nest/dist/src/main.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:927:15)
Expected behavior
Expected import bson,dont bson/bson
Minimal reproduction of the problem with instructions
What is the motivation / use case for changing the behavior?
npm i npm run start
Environment
Nest version: 8.0
For Tooling issues:
- Node version: node 16
- Platform: mac 15
Others:
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 7
- Comments: 20 (3 by maintainers)
This appears to be an issue with the CLI Plugin. I was able to solve this issue by removing the
@nestjs/swaggerplugin declaration from thenest-cli.jsonfile. We have declared decorators manually so the plugin was not needed. As soon as it was removed, this problem went away.Also i noted
Doing this does not produce any error . However on other cases swagger plugin tries to introspect the
ObjectIdtype and thats whenbson/bsonlib collision occursOne workaround is to install ‘bson’ version 1.1.5 directly and use:
import {ObjectId} from 'bson';instead ofimport {ObjectId} from 'mongodb'}Another workaround is to not have prefix in the entity filename.
So instead of
common.entity.jsjust useentity.jsnot ideal I know but it worksDid you run mongodb instance locally? Without that the orm wont bootup You can see here the logs https://imgur.com/a/K7VbNCL @micalevisk
I managed to fix this issue by adding @agravelot’s custom script in package.json and forcing it to run after nest build script like so.
@micalevisk here is a reproduction https://github.com/rubiin/nest-swagger-bug . Basically problem from file
common.entity.tsYeah. I will try to provide a minimal reproduction asap
@rubiin can you share a minimum reproduction of that?
why reproductions are required
I am experiencing the same issue, when using abstract or just extending another class.