Skip to content
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

feat: make generated require() ESM compatible #3235

Merged
merged 1 commit into from
Jan 16, 2025

Conversation

bdezso
Copy link
Contributor

@bdezso bdezso commented Jan 12, 2025

PR Checklist

Please check if your PR fulfills the following requirements:

  • The commit message follows our guidelines: NestJS Contribution Guide
  • Tests have been added (for bug fixes and/or features)
  • Documentation has been added or updated (for bug fixes and/or features)

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional or API changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

NestJS does not officially support ESM. I have a project that successfully uses ESM with a custom fork, but in this Swagger plugin, the only incompatibility I found is that the generated code uses require without file extensions.

  • Prior to Node.js 22, require could not import ES modules.
  • As of Node.js 22, require can import ES modules, but missing file extensions continue to cause issues.

Issue Number: N/A

What is the new behavior?

This PR adds the appropriate file extension in the generated code based on the input extension (.cts, .mts, or .ts), thereby improving compatibility with ESM.

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

I am a new NestJS user who has integrated it into an existing ESM-based project. Currently, I rely on a monkey-patch workaround, but I would prefer to avoid such hacky solutions. This PR addresses that concern by providing a more robust and maintainable approach.

@bdezso bdezso force-pushed the esm-compatible-require branch from 4b17f42 to 12cc1cf Compare January 12, 2025 22:03
@bdezso bdezso changed the title feat: make generated require ESM compatible feat: make generated require() ESM compatible Jan 12, 2025
Copy link
Member

@micalevisk micalevisk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please revert the changes made on package-lock.json file

@bdezso bdezso force-pushed the esm-compatible-require branch from 12cc1cf to 43eb567 Compare January 14, 2025 21:56
@bdezso
Copy link
Contributor Author

bdezso commented Jan 14, 2025

I removed package-lock.json from my commit.

@kamilmysliwiec kamilmysliwiec merged commit fac4265 into nestjs:master Jan 16, 2025
1 check passed
@kamilmysliwiec
Copy link
Member

LGTM

@kamilmysliwiec
Copy link
Member

Reverting as it introduced a major regression #3251

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants