Skip to content

feat!: prepare ESM-only v3 and drop NestJS 8/9 support - #65

Merged
WhiteKiwi merged 7 commits into
mainfrom
feat/v3-esm-only
Oct 6, 2026
Merged

WhiteKiwi merged 7 commits into
mainfrom
feat/v3-esm-only

Conversation

@WhiteKiwi

@WhiteKiwi WhiteKiwi commented Oct 1, 2026 •

Copy link
Copy Markdown
Member

Summary

Prepare the next major release on top of the 2.2.1 main-branch baseline. NestJS 12 support was introduced in 2.2.0. Related to #59; independent of the typed-decorator work in #63.

  • Drop NestJS 8/9 peers; retain NestJS 10/11/12.
  • Ship one ESM build with type: module, explicit types/default root export (no legacy main), NodeNext build resolution and .js relative specifiers.
  • Propose Node.js >=22.12.0 as the new runtime floor.
  • Keep decorator runtime behavior intact; make erased imports explicit.
  • Use native ESM Jest/Prettier configuration files and a NodeNext root TypeScript config.
  • Keep the migration guide under docs/migrations/v3.md in the repository only, with maintained main-branch README/changelog links. Publish only dist, retain top-level types, build in prepack, and embed sources in JS source maps without shipping the source tree or declaration maps.
  • Add packed-tarball consumer CI for Nest 10.4.22/11.2.6/12.1.0 × Node 22.12.0/24.19.0/26.10.0, plus baseline and latest-per-major Nest cases on Node 24.19.0.

Release boundary

Draft only: do not merge or publish until the support policy below is reviewed. The package version remains 2.2.1; changeset status reports one major release to 3.0.0. No version command or publish was run.

Includes main be825e60e62351dbc222030d57c623349317d0db (2.2.1, including #66 and #67), integrated without rewriting the reviewed branch history. PR #63 is not included. If it merges first, rebase and rerun the consumer matrix; do not inadvertently combine an intended separate minor release into the major.

Published 2.2.0 baseline (fresh npm consumer)

On Node 24.19.0, independently installed @toss/nestjs-aop@2.2.0 + Nest 12.1.0 peers, with no workspace symlinks:

  • Native CJS and ESM applications pass actual Nest bootstrap/aspect wrapping, before/original/after order, return values, undecorated method, lifecycle completion and repeated-call checks.
  • Jest 30.4.2 CJS passes with --experimental-vm-modules; default Jest without the flag fails on Nest's ESM.
  • Static ESM imports hit Jest's mixed CJS/ESM concurrent-loader error. Sequentially awaiting Nest common/core imports before dynamically importing the consumer passes.
  • The v3 candidate passes that static-import ESM Jest fixture directly.

Candidate verification

Current head: 2e1b7f486d69d3e7152013e708b94adc3ac2a93d.

  • Frozen-lockfile install, lint, root NodeNext typecheck (including tests), build and pack pass. Four focused source-map regression tests also pass.
  • Repository Jest suite: 5 suites / 22 tests / 12 snapshots pass.
  • CI #129 passes all 16 jobs: the base job, all 9 pinned Node/Nest pairs and 6 baseline/latest packed-consumer jobs.
  • Every packed-consumer job verifies native CJS/ESM runtime, plain-JS ESM Jest, the migration guide's exact ts-jest ESM configuration with Jest 29.7.0 and 30.4.2, compiler compatibility boundaries, and the dist-only/source-map packaging contract.
  • TypeScript 5.8.3 NodeNext .mts/.cts, TypeScript 5.7.3 NodeNext .mts, and both versions in CommonJS/node10 mode pass. Expected TS1479 failures are asserted for TypeScript 5.7.3 CJS/NodeNext and TypeScript 5.8.3 CJS/node16.
  • CJS Jest 30.4.2 with the VM flag passes on Node 24.19/26.10. Node 22 CJS Jest remains explicitly migration-required and visibly skipped after validating ESM tests; it is not claimed as a pass.
  • The new TypeScript fixture checks actual Nest bootstrap, constructor injection, aspect order, return values, lifecycle and repeated calls. The guide/config comparison works with LF and CRLF checkouts.
  • Local Node 24.19 checks cover root NodeNext typechecking and all 22 tests on Nest 10.4.22, 11.2.6 and 12.1.0, plus the four map regressions and packed Nest 12.1 consumer. The full pinned and baseline/latest matrices pass in CI.

Review follow-up

  • Document the conditional TS 5.8 floor for CJS/NodeNext, node16/node18 limitations and the existing CommonJS/node10 path. Jest 29 remains supported in ESM mode; upgrading to Jest 30 is not universally required.
  • Keep ESM-only packaging, Nest 10/11/12 peers and Node >=22.12. Document the Node 22.12 require(esm) warning rather than changing the runtime floor.
  • Replace stale commit-pinned published documentation links with maintained main-branch links and remove PR-process notes from the migration guide.
  • Keep reproducible pins and add baseline/current-major checks. The baseline pairs are 10.0.0, 11.0.0 and 12.0.1. Nest 12.0.0 has inconsistent upstream peers (core requires common ^11), so 12.0.1 is used without bypassing peer validation.
  • The Nest 10.0.0 baseline explicitly uses skipLibCheck:true for its upstream file-validator TS2416 errors. Application import/compiler-boundary checks still run; the other pinned/latest cases retain skipLibCheck:false.
  • Pack under the matrix Node version. The verifier can invoke npm's JS entry point through npm run test:packed, avoiding Windows .cmd execution. Windows itself was not run locally.
  • Remove unused Babel configuration/devDependencies, stale esm exclusions and the Nest 8 comment. Lockfile pruning removes 116 unused entries without changing retained dependency versions.
  • Keep the root-only export contract, existing v2 guide path and npm-based prepack build. No dual build, package.json subpath export or unrelated runtime change is introduced. The existing Aspect/Injectable defect was fixed separately in fix: apply injectable metadata to aspect classes #66 and is now included through the main integration.

Second review follow-up

  • Integrate main's 2.2.1 release and resolve package.json while retaining the ESM root exports and no legacy main field.
  • Update the imported Aspect regression test's four relative imports to .js, make LazyDecorator/WrapParams type-only, and use @nestjs/common/constants.js for NodeNext compatibility with Nest 10/11 as well as 12.
  • Add root source/test typechecking and packaging-regression gates to the base job and all nine pinned compatibility jobs. These cover test files excluded from the package build.
  • Scan source maps recursively, including dist/core and dist/utils. The packed verifier reports all nine JavaScript maps checked. Four red/green tests cover traversal, nested missing embedded source, nested absolute paths and nested declaration maps.

Proposed support contract — maintainer decision

Keep Node >=22.12 runtime support and require Node 22 users to migrate Jest tests to ESM mode. Modern Node 24.9+ / recent Jest may use synchronous ESM loading with the VM flag, but old CJS test/mocking workflows are not universally compatible.

This policy is a proposal in this draft, not a final approval of the migration cost. No dual build is introduced. The migration guide also records the exports/deep-import break and ESM mock differences.

Why this draft uses one ESM artifact

A dual build is a viable alternative for preserving Nest 10/11's default CommonJS toolchain. This draft favors one implementation artifact for both import and supported require consumers, avoiding separate CJS/ESM module copies and parallel build/export/declaration outputs. The cost is the explicitly documented compiler and Jest migration.

With the current ASPECT = Symbol('ASPECT'), independently loaded CJS and ESM builds would not share the aspect metadata key. A dual-build implementation would need a namespaced shared key such as Symbol.for, plus mixed-loader regression tests; the hazard is manageable, not a claim that dual publishing is impossible. Maintainers still need to decide whether this single-artifact tradeoff is appropriate before merging.

Pure-ESM packaging references

Reviewed current p-queue (TypeScript), p-limit, p-retry and Sindre Sorhus's guide.

Adopted type:module, types/default exports in that order, no legacy main, dist-only allowlist, a top-level types pointer, Node-aware compiler resolution and prepack build. Keep the tested Node floor and Nest legacy decorator metadata rather than copying unrelated compiler/test presets. ES2022 output retains the previous class-field semantics explicitly. No blanket sideEffects:false assertion is added to this decorator/metadata library.

Local build, lint, 22 tests and fresh packed Nest 12 consumer pass. Packed artifact has 30 files, no source/doc trees or declaration maps, and all 9 JS maps contain embedded source without absolute source paths. The CI consumer test now also enforces this packaging contract.

CI

CI #129 passed on exact head 2e1b7f486d69d3e7152013e708b94adc3ac2a93d: all 16 jobs, including 15 packed-consumer jobs with the expanded compiler/Jest and packaging checks.

The PR remains draft and has not been merged into main. No version command or publish was run for this proposal. The support-policy decision above is still required before release.

@dleo9307 dleo9307 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Review summary

Recommendation: request changes (policy decision needed before merge). The code changes themselves are clean, and the runtime behavior is unchanged. The concern is an inconsistency in the support policy: NestJS 10/11 stay in peerDependencies, but the package ships in a format that breaks the default toolchain for those versions (the Nest CLI starter template).

CI is green on all 10 jobs, and I reproduced lint, build and 19 tests / 12 snapshots locally (Node 24.19.0, pnpm 11.23.0).

What I measured

I built a project shaped like the Nest 11 CLI starter: CommonJS package, module: nodenext, Jest 29.7 + ts-jest, "test": "jest". I installed the published v2.2.0 from npm and this PR's packed tarball into it, and compared them on Node 24.19.0.

Scenario v2.2.0 v3 (this PR)
tsc (TS 5.7.3, nodenext) ✅ ❌ TS1479
tsc (TS 5.8.3, nodenext) not measured ✅
tsc (TS 5.8.3, node16) not measured ❌ TS1479
jest 29.7, no flag ✅ ❌ Jest encountered an unexpected token
jest 29.7 + --experimental-vm-modules not measured ❌ Must use import to load ES Module
jest 30.4, no flag not measured ❌ SyntaxError: Unexpected token 'export'
jest 30.4 + VM flag not measured ✅
Nest 10 starter shape (module: commonjs): tsc, then run not measured ✅
Run on Node 22.12.0 not measured ✅ but prints an ExperimentalWarning
Run on Node 22.13.0 / 22.14.0 not measured ✅ no warning

🔴 Decision needed before merge

1. NestJS 10/11 are listed as supported, but their default template breaks

  • What a Nest 11 user must change:
    • Upgrade TypeScript to 5.8 or later, and use nodenext (not node16).
    • Upgrade Jest from 29 to 30, and add the VM flag to the test command.
    • Even then, Node 24.9 or later is required. On Node 22, the Jest setup has to move fully to ESM mode, including changes to how mocks are written (jest.unstable_mockModule, etc.).
  • What it gains: as the baseline in the PR description shows, v2.2.0 already works on Nest 12 in both CJS and ESM apps. The only newly fixed case is "Nest 12 + Jest ESM + static import", and that case already has a workaround. The related issue #59 asked for Nest 12 installability; 2.2.0 already closed it. It did not ask for ESM.
  • Options:
    • (a) Dual build: ship CJS and ESM with separate import / require conditions in exports. This fixes the Nest 12 Jest ESM case without breaking Nest 10/11.
      • One prerequisite: ASPECT = Symbol('ASPECT') in src/aspect.ts:3 is created separately per module copy. If the CJS and ESM copies are both loaded in one process, AutoAspectExecutor from the CJS copy cannot find metadata set by @Aspect from the ESM copy. It would need to become Symbol.for('@toss/nestjs-aop/ASPECT').
    • (b) To keep v3 ESM-only, narrow peerDependencies to ^12 and state clearly: v3 is for Nest 12; Nest 10/11 stay on v2.
    • (c) I'd avoid the current option: Nest 10/11 stay "supported" while their default template breaks.

🟠 Fix before release

2. The guide has no TypeScript floor, and CI hides the problem

  • scripts/verify-packed.mjs:33 pins typescript@5.8.3. The .cts fixture only passes because TS 5.8 added require(esm) support. On TS 5.7 or earlier, or with module: node16, it fails with TS1479.
  • docs/migrations/v3.md only says "CommonJS applications may use require()". It should state the conditions:
    • With module: commonjs (node10 resolution, using the top-level types field), it works.
    • With nodenext, TS 5.8 or later is required.
  • I'd also add a module: commonjs case and a TS 5.7 failure case to CI.

3. The Node 22.12.0 floor prints a warning

  • On Node 22.12.0, a CJS app prints this warning at runtime when it require()s the package. From 22.13.0 onward it does not.
    ExperimentalWarning: CommonJS module ... is loading ES Module ... using require().
    Support for loading ES Module in require() is an experimental feature
    
  • I suggest raising engines to >=22.13.0 and updating the CI matrix, the changeset and the guide to match. If 22.12.0 stays, the warning should at least be documented.

4. The migration guide links are pinned to a commit on the PR branch

  • README.md:67, readme_kr.md:67 and .changeset/modern-aspects-migrate.md:10 all point to blob/f467f9c…/docs/migrations/v3.md.
  • According to CONTRIBUTING, this repo squash-merges PRs, so this commit will never land on main:
    • The link shows a "This commit does not belong to any branch" banner.
    • Later fixes to the guide will not show up.
    • The changeset text stays permanently in the CHANGELOG and the GitHub Release.
  • I suggest blob/main/... or a release-tag path (blob/v3.0.0/...). The language link in the same README already uses blob/main/.
  • The pinned guide also contains PR-process notes. These should not be in a user-facing document:
    • "The package version stays at 2.2.0 in this implementation PR"
    • "Merge and publish v3 only after reviewing…"
    • The notes about #63
    • The "Packaging references" section

🟡 Suggestions

  • CI no longer catches regressions in new Nest releases (ci.yml):
    • The previous ^N followed the latest release of each major; the matrix is now pinned to 10.4.22 / 11.2.6 / 12.1.0.
    • ^10 claims support for all of 10.x, which no longer matches the "verified claim" comment above the matrix.
    • I'd keep the pinned versions for reproducibility and add one latest-per-major case.
  • CI does not verify the guide's ts-jest ESM config. The Jest cases in verify-packed run plain .mjs/.cjs files with transform: {}. Adding the ts-jest path that Nest users actually use would let CI catch the guide's example breaking.
  • "ESM only" in the README is misleading. CJS apps do work through require(esm), but the wording reads as "can't be used from CJS". I'd put the TS floor and the Jest requirement (Jest ESM mode, or Jest 30 + VM flag) on the README requirements line.
  • pnpm pack in CI rebuilds with the default Node, not the matrix Node. prepack rebuilds with the default Node (24.19), so the tarball under test isn't built by the matrix Node, and the earlier Build step is duplicated. tsc output doesn't vary by Node version, so the impact is small.
  • exports has no "./package.json" entry. Any tool that reads @toss/nestjs-aop/package.json directly will fail (optional).

⚪ Nits

  • @types/node is ^20, while engines is >=22.12.
  • .babelrc and the two @babel/* devDependencies aren't used anywhere. The esm entries left in the tsconfig exclude and the eslint ignores are leftovers from an earlier dual build. A major release is a good time to clean these up.
  • The comment at src/auto-aspect-executor.ts:54 ("for support nestjs 8") is stale now that Nest 8 support is dropped.
  • scripts/verify-packed.mjs:
    • The MIGRATION_V3.md check at :110 is dead code left over from an earlier iteration.
    • The win32 npm.cmd branch at :18 doesn't work anyway: recent Node versions throw EINVAL when execFileSync runs a .cmd file without a shell.
    • The two ifs with opposite conditions at :68 / :77 could be one if / else.
  • The existing guide is docs/migration-guide-v2.md, but the new one is docs/migrations/v3.md, so the path conventions differ.
  • prepack uses npm run build in a pnpm repo.

✅ What's good

  • The src changes only add .js to import paths and make import type explicit, so runtime behavior is unchanged.
  • Raising the target to ES2022 flips TS's default for class fields, and setting useDefineForClassFields: false explicitly keeps the existing class-field behavior. Good call.
  • The type import from @nestjs/core/injector/instance-wrapper.js is erased at build time and doesn't leak into the shipped .d.ts (checked dist/auto-aspect-executor.d.ts).
    • In Nest 12, InstanceWrapper moved to @nestjs/core/internal and isn't exported from the package root.
    • So this is the only path that works across Nest 10, 11 and 12, which makes it a reasonable choice.
  • The tarball contains only dist (30 files). The source maps embed the original sources, and there are no .d.ts.map files.
  • CI that installs the actual tarball into independent projects (what users receive, not workspace symlinks) is a clear improvement.

Out of scope (existing bug, separate PR suggested)

applyDecorators(SetMetadata(ASPECT, metadataKey), Injectable) in src/aspect.ts:10 passes the Injectable function itself instead of calling Injectable(). As a result, classes decorated with @Aspect never get the __injectable__ metadata (checked: undefined, versus true when @Injectable() is applied directly). It works today only because SetMetadata is itself a decorator, so design:paramtypes is still emitted.

Copy link
Copy Markdown
Member Author

@dleo9307 Thanks for the detailed review. I addressed the documentation and coverage gaps in 1dd51beb:

  • Documented the TS >=5.8 requirement specifically for CJS + NodeNext, the node16/node18 limitations, and the existing CommonJS/node10 path. Added positive and expected-failure compiler checks.
  • Packed consumers now run the guide's exact ts-jest configuration with Jest 29.7 and 30.4.2. Jest 29 works in ESM mode; retaining CJS test output uses the tested Jest 30.4.2 + Node >=24.9 + VM-flag setup. Node 22 uses ESM tests.
  • Kept the pinned matrix and added baseline/latest-per-major Nest coverage, with the upstream baseline-version exceptions documented.
  • Updated published guide links to main, removed PR-process notes and unused Babel/stale configuration, and made packing use the matrix Node version.

I kept the proposed ESM-only / Nest 10–12 / Node >=22.12 policy: native CJS/ESM applications and the configured tests pass on Nest 10/11, so narrowing the peers would exclude working combinations. The default-starter migration cost is real; the requirements are now explicit, and this remains a draft pending the support-policy decision. The Node 22.12 warning is documented rather than raising the floor.

The existing Injectable() issue was fixed separately in #66, now merged. CI #127 passed all 16 jobs on 1dd51be.

@dleo9307

dleo9307 commented Oct 2, 2026

Copy link
Copy Markdown

@WhiteKiwi Thanks for the quick and thorough follow-up. I checked 1dd51beb against the earlier points: the TS-floor docs and the expected-TS1479 checks, the guide config being diffed against what CI runs, compat-range, the blob/main links and the cleanups all look good. Three new things came up while verifying, plus one open question.

1. The branch now conflicts with main, and rebasing pulls in a test file that fails NodeNext type-checking

main now has #66 and #67 (2.2.1). The textual conflict is only the version line in package.json. I resolved it locally (kept 2.2.1 and no main field), and lint, build and all 22 tests pass.

However, the new src/__test__/aspect.test.ts from #66 (lines 6–9) uses extensionless relative imports:

$ npx tsc -p tsconfig.json --noEmit
src/__test__/aspect.test.ts(6,27): error TS2307: Cannot find module '../aop.module' or its corresponding type declarations.
src/__test__/aspect.test.ts(7,32): error TS2835: Relative import paths need explicit file extensions ... Did you mean '../aspect.js'?
(same for ../create-decorator and ../lazy-decorator)

CI won't catch this:

  • Jest overrides the compiler options with moduleResolution: 'bundler'.
  • tsconfig.build.json excludes __test__.

So the errors would only show up as editor errors. On the PR head alone, the same command exits 0. When rebasing, I'd suggest:

  • Add .js to those four imports, and use import type for LazyDecorator / WrapParams.
  • Consider adding tsc -p tsconfig.json --noEmit to the lint step so test files stay NodeNext-clean.

2. The source-map assertion doesn't check subdirectories

scripts/verify-packed.mjs:188 uses readdirSync(join(packageDir, 'dist')), which isn't recursive. So the maps under dist/core/ and dist/utils/ (3 of the 9) are never checked. readdirSync(..., { recursive: true }) would cover them.

3. Remaining nits (optional)

  • @types/node is still ^20 while engines is >=22.12.0.
  • There's no "./package.json" entry in exports.
  • The guide paths differ: docs/migration-guide-v2.md vs docs/migrations/v3.md.
  • prepack uses npm run build in a pnpm repo.

Open question on the policy

Your point about narrowing the peers is fair: Nest 10/11 apps do run, and the requirements are now explicit. One option still has no answer, though. Was a dual build considered? It would fix the Nest 12 Jest ESM case without changing anything for Nest 10/11 default-starter users.

If it's ruled out, it would help to record the reason in the PR or the guide, for example the dual-package hazard. If it's ever revisited, ASPECT = Symbol('ASPECT') in src/aspect.ts would need to become Symbol.for(...). Otherwise a CJS copy and an ESM copy loaded in the same process wouldn't see each other's aspect metadata.

I'm happy to leave the final policy call to the maintainers.

WhiteKiwi commented Oct 2, 2026 •

Copy link
Copy Markdown
Member Author

@dleo9307 Thanks for re-checking. Both new issues are fixed in 2e1b7f4:

  • Synced main's 2.2.1 baseline, keeping the ESM exports and no legacy main field. Fixed the four relative imports and the type-only import. Checking Nest 10/11 also exposed the extensionless @nestjs/common/constants import, so that now uses constants.js too.
  • Added root NodeNext typechecking, including test files, to the base job and all nine pinned compatibility jobs.
  • The map checker now recurses through dist/core and dist/utils and reports all nine JavaScript maps scanned. Four red/green regression tests cover nested traversal, missing embedded source, absolute paths and declaration maps.

CI #129 passed all 16 jobs on this head. Local typechecks and all 22 tests also pass on Nest 10/11/12.

For v3, I've chosen the ESM-only direction for this PR, considering the move toward ESM-only publishing among libraries and the maintenance benefit of a single build. One implementation artifact for import and supported require consumers keeps the build, exports, declarations and compatibility testing simpler. Nest 10/11 remain supported with the tested Node, TypeScript and Jest configurations. This deliberately accepts the compiler and Jest migration cost for existing CJS setups as a documented major-version change.

Dual builds remain technically viable and would preserve more of the default CJS toolchain. The current ASPECT symbol would require a namespaced shared key such as Symbol.for, plus mixed-loader regression tests. That is a manageable tradeoff; the single-build approach is the direction I've chosen here.

The optional nits remain unchanged, and the PR stays draft for maintainer review.

@WhiteKiwi
WhiteKiwi marked this pull request as ready for review October 6, 2026 09:33
@WhiteKiwi
WhiteKiwi merged commit 3971571 into main Oct 6, 2026
16 checks passed
@WhiteKiwi
WhiteKiwi deleted the feat/v3-esm-only branch October 6, 2026 09:33
@github-actions github-actions Bot mentioned this pull request Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants