swagger icon indicating copy to clipboard operation
swagger copied to clipboard

Using type enum from third party imported npm package causes "is not defined by 'exports'"

Open tobias-jueschke opened this issue 2 years ago • 8 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Current behavior

I have a simple nestjs dto class importing an enum type from a third party npm package.

import { OrderStatus } from '@company/common';

export class OrderListItemDto {
  @IsEnum(OrderStatus)
  @IsNotEmpty()
  public status: OrderStatus;
}

In the @company/common modul the enum is exported as follows:

export enum OrderStatus {
  NEW = "new",
}

running the app cause the following error:

Error: Package subpath './dist/enums/order-status.enum' is not defined by "exports" in /home/user/data/oms-order-service/node_modules/@company/common/package.json
    at new NodeError (node:internal/errors:371:5)
    at throwExportsNotFound (node:internal/modules/esm/resolve:453:9)
    at packageExportsResolve (node:internal/modules/esm/resolve:729:3)
    at resolveExports (node:internal/modules/cjs/loader:482:36)
    at Function.Module._findPath (node:internal/modules/cjs/loader:522:31)
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:919:27)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Function._OPENAPI_METADATA_FACTORY (/home/user/data/oms-order-service/dist/orders/dtos/order-list-item.dto.js:20:202)

If I change the type of the property to stringand keep the import the app start successfully. If I use the enum not in *.dto files the enum works fine.

Steps to reproduce

https://stackblitz.com/edit/nestjs-typescript-starter-bbgorc?file=package.json,src%2Fmain.ts,nest-cli.json,src%2Fdtos%2Fcats.dto.ts,src%2Fapp.controller.ts,src%2Fapp.service.ts

Expected behavior

APP runs with imported enum from third party npm package.

Package version

6.0.5

NestJS version

9.0.9

Node.js version

16.14.0

In which operating systems have you tested?

  • [ ] macOS
  • [ ] Windows
  • [X] Linux

Other

No response

tobias-jueschke avatar Sep 16 '22 21:09 tobias-jueschke

I found out that the nestjs/swagger cli plugin causes the problem. If I remove the plugin it works as expected.

tobias-jueschke avatar Sep 19 '22 05:09 tobias-jueschke

Please provide a minimum reproduction repository (Git repository/StackBlitz/CodeSandbox project).

kamilmysliwiec avatar Sep 19 '22 07:09 kamilmysliwiec

Here is the link to reproduction https://stackblitz.com/edit/nestjs-typescript-starter-bbgorc?file=package.json,src%2Fmain.ts,nest-cli.json,src%2Fdtos%2Fcats.dto.ts,src%2Fapp.controller.ts,src%2Fapp.service.ts

tobias-jueschke avatar Sep 19 '22 09:09 tobias-jueschke

Having the same issue Looks like nest js swagger plugin creates code in a compiled bundle like "require("package/internal")"

aqemi avatar Feb 10 '23 16:02 aqemi

Im experiencing the same issue when using monorepo and importing DTOs from shared packages.

For the DTOs i found a workaround.

instead

import { barDto } from '@shared/dtos'

export class fooDto {
  bar: barDto;
}

you can create new empty class in your app and just extend from original DTO

import { barDto as barDtoExternal } from '@shared/dtos'

class barDto extends barDtoExternal {}

export class fooDto {
  bar: barDto;
}

gradam avatar Sep 20 '23 05:09 gradam

Did anyone solved this issue? I'm having the same problem with shared enums...

DavidReinberger avatar Nov 28 '23 15:11 DavidReinberger

I also encountered similar problem, im my case the package path is absolute path. I move the project to a none unicode character path and it work.

bibitoo avatar May 22 '24 13:05 bibitoo

https://stackblitz.com/edit/nestjs-typescript-starter-bbgorc?file=package.json,src%2Fmain.ts,nest-cli.json,src%2Fdtos%2Fcats.dto.ts,src%2Fapp.controller.ts,src%2Fapp.service.ts

@kamilmysliwiec the description has a stackblitz and this is still an issue.

icco avatar Jun 20 '24 14:06 icco