-
Notifications
You must be signed in to change notification settings - Fork 471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compiler error "ReferenceError: require is not defined in ES module scope, you can use import instead" #2925
Comments
I'm having this issue as well in my ESM output service. The issue is with non-literal types like objects and enum, those are translated to a dynamic import by the plugin swagger/lib/plugin/utils/plugin-utils.ts Line 160 in 29f59e9
swagger/lib/plugin/utils/plugin-utils.ts Line 207 in 29f59e9
I tried to remove those two lines and it runs perfectly fine. So technically it is not blocked by the "ESM support" issue, just making it not do the string replace if the target is ESM |
I found this package to do monkey patch on the build, not sure how to integrate it to the cli with watch mode capability It references microsoft/TypeScript#43329 when mentioning this "require" import issue |
Is there an existing issue for this?
Current behavior
I found this bug when I add Swagger to one of my services. In that one, the problem arises with a DTO 👍
Here "StorageConf" is an Enum. It works OK when it is a string (comment one line and uncomment the other).
When creating the sample to share in the repo with the "minimum reproduction code", the issue generalised. It happens always in that code, and it is really a very basic one.
Important points:
the javascript code created in "dist" for the DTO above is:
The problem is with the 'require" inside _OPENAPI_METADATA_FACTORY()
This issue may be linked to ESM Build Issues #1450 , which seems to depend on build: generate multiple formats (cjs, esm) #8736 , which points to this recent post in Joyee Cheung's Blog. I did try running with flag --experimental-require-module , but it did not make any difference.
The issue is not consistent. In the provided sample repo, it happens always.
In a larger project that I have with several independent micro-services (separated in different apps), all set to ES2022, it only happens with the one that has the DTO above, all other compile correctly. Actually, as mentioned, even this one compiles correctly if "storageconf" is of type string instead of "StorageConf" enum.
Minimum reproduction code
https://github.com/yaluba1/NestJsSwaggerBug
Steps to reproduce
Expected behavior
I was expecting correct compilation. Instead. I get:
Package version
7.3.1
NestJS version
10.3.0
Node.js version
20.11.0
In which operating systems have you tested?
Other
IDE: VsCode vs1.88.1
Package manager: yarn v4.1.1
The text was updated successfully, but these errors were encountered: