-
Notifications
You must be signed in to change notification settings - Fork 483
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
ESM Build Issues #1450
Comments
Would you be able to provide a very simple, minimal reproduction repository? This would let us address this issue quicker. |
Hello! I got this error too, and also notice another thing: import { HealthCheckResponse } from '../resource/health-dto';
import { Controller, Get } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
@Controller('health-check')
@ApiTags('Health')
export class HealthController {
@Get()
getHealth(): HealthCheckResponse {
return {
status: 'pass',
};
}
} The generated decorators code: __decorate([
Get(),
openapi.ApiResponse({ status: 200, type: require("../resource/health-dto").HealthCheckResponse }),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", HealthCheckResponse)
], HealthController.prototype, "getHealth", null);
HealthController = __decorate([
Controller('health-check'),
ApiTags('Health')
], HealthController);
export { HealthController }; Notice that we got a require call, which will throw an error when using ESM, as it's not allowed. Though it's not a major issue for our team as we're still sticking with commonJS, it'll be as node 16 adoption increases and more and more developers start to adopt ESM in their packages, like this one Edit: __metadata("design:returntype", HealthCheckResponse) And, this way, you don't need to worry about the module type |
Any updates? |
This comment was marked as spam.
This comment was marked as spam.
I understand you're desperate for us to start supporting ESM. Please calm down on how you request we support this. I've seen your messages across three different issues so far, and none of them come off as professional or kind. We are working on this, but it appears not as fast as you'd like. If you really really need this now, feel free to make a Pull Request to help us out. |
This comment was marked as spam.
This comment was marked as spam.
I'm not sure if it's related, but I seem to have it this issue when changing my When the Lambda runs (behind an API gateway), it explodes with the following error:
This is having used webpack to bundle a single file, with Webpack config value Changing the I suspect this is because the Swagger code is never actual invoked as a Lambda, it's only annotations that live on my DTO objects, but a swagger endpoint is not activated when running as a Lambda endpoint. The actual Swagger use is for dev servers/local development, which is a separate file/entry point. So something about the treeshaking is omitting Edit: This only seems to effect the bundle deployed to Lambda. Running a dev server (which granted, is a different entry file) works just fine locally. |
Has there been any updates regarding ESM imports? I'm having a similar issue that was referenced at the top of this thread and am trying to solve it. |
I have had no problems importing pure ESM packages in my NestJS project. Sure it can get a little tricky and there are probably situations where it would be really nice to have ESM support in NestJS, but dynamic imports work like magic in the meanwhile. |
Switching my nestjs application to an es6 module works great other than this import. I have other dependencies that require es6 so I guess I'm stuck removing this import sadly.. Any workarounds? ** EDIT ** |
Here is the optimistic monkey patch: https://dev.to/antongolub/nestjs-esbuild-workarounds-99i |
I've been trying hard to get my Swagger setup working, with the plugin, but so far I've only been successful with manual
Apparently some decorators are indeed added to my bundle even if I don't annotate with ApiProperty (plugin works?), but now my output JS uses I tried following @antongolub monkeypatch advice and it helped with several issues. Due to dto being bundled, I had to manually actually remove the converted imports (which in my case were just as unnecessary as In the end, after one full day, I didn't manage to get the plugin to work with my project. A bunch of observations:
|
Minor breakthrough: I managed to get ESM working by commenting out swagger/lib/plugin/utils/ast-utils.ts Line 109 in 1daf8e0
This changes the I still got some errors because the Of course, the Update: This solution doesn't work if the import was renamed (for example: |
I wrote a proof of concept that checks if the type already exists in the file (either defined in the same file or imported from another file), it will reuse it instead of emitting Limitations:
If anyone wants to add tests and submit a pull request based on my changes, feel free to do so. |
I might be naive here, but is there a path forward with SWC integration via nestjs/nest-cli#2107? ie using SWC to transpile from ESM to commonjs typescript, including references specified in tsconfig which are ESM, such that the |
How did any of you manage to get the swagger cli plugin working at all without using commonjs? |
@Hareloo We ended up removing the plugin altogether and manually added the swagger decorators to all controllers and dtos. |
Two years later and this is still the blocker for converting all of our projects to esm, which is a requirement for a lot of newer tooling. |
@mr-short more than 95% of Node.js ecosystem is still running on CJS. |
No need to keep this open - we'll revisit one day once the NestJS core is migrated to ESM too |
I'm submitting a...
Current behavior
Using anything other than
commonjs
(ES6
etc) does not generate the proper js code.Using
nest build
: causesReferenceError: openapi is not defined
when trying to run the code.Using
tsc
: can run code but does not auto generate any of the dto schema properties (just shows an empty object).Expected behavior
nest build
would create error free code, and auto generate proper swagger/openapi docs for the api using ESM compilation.Minimal reproduction of the problem with instructions
Repro repo: https://github.com/mr-short/nestjs-esm-test
Error 1
"module": "ES6"
in thetsconfig.json
nest build
Error 2
"module": "ES6"
in thetsconfig.json
tsc
What is the motivation / use case for changing the behavior?
My organization's projects use ESM and we are migrating existing large Expressjs projects over to Nestjs.
Environment
The text was updated successfully, but these errors were encountered: