juan noguera
juan noguera

Reputation: 159

Nestjs with OpenAPI 'ReferenceError: require is not defined'

I have a NestJS project in which I have just added support for OpenApi (Swagger) as described in the documentation. I use the CLI plugin for convenience. The tsconfig.json is set as follows:

"compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ES2022",
... }

and package.json is set to use "type": "module".

One of the modules implements a CRUD controller and the DTO looks like this:

export class CreateOrgDto {
    name: string;
    country: string;
    lang: string;
    storageconf: StorageConf;
}

where 'StorageConf' is an enum.

With this, I get error ReferenceError: require is not defined during compilation. But with storageconf: string, it works OK.

The problem seems to be with the created js code in the 'dist' directory. There, I see this:

import { createRequire as _createRequire } from "module";
const __require = _createRequire(import.meta.url);
const openapi = __require("@nestjs/swagger");
export class CreateOrgDto {
    ...
    storageconf;
    static _OPENAPI_METADATA_FACTORY() {
        return { ... , storageconf: { required: true, type: () => String, enum: require("../entities/org.entity", { with: { "resolution-mode": "import" } }).StorageConf } };
    }
}

inside _OPENAPI_METADATA_FACTORY(), it is using the 'require()' function instead of the '__require' defined in the top.

I suspect that this may be a bug in package "@nestjs/swagger", but could also be some grey setting that I can't find. Any pointer on possible places to look?

PS: I would like to keep this project as module with the set target.

Upvotes: 0

Views: 80

Answers (0)

Related Questions