Skip to content

fix: resolve require() in ES modules with the require condition - #626

Merged
styfle merged 4 commits into
vercel:mainfrom
RClaudiuM:fix/621-require-in-esm-cjs-resolve
Nov 19, 2026
Merged

styfle merged 4 commits into
vercel:mainfrom
RClaudiuM:fix/621-require-in-esm-cjs-resolve

Conversation

@RClaudiuM

Copy link
Copy Markdown
Contributor

Fixes #621

What

A require() call inside an ES module (through module.createRequire, or the global require with mixedModules) was resolved with the import condition. emitDependency resolved every specifier in deps with cjsResolve = !isESM, and in an ES module deps held both the static import/export ... from declarations and the require() calls. For a dual package whose exports subpath has separate import and require targets, nft traced the ESM build while Node loads the CommonJS one, so the deployed function fails with MODULE_NOT_FOUND.

Fix

  • analyze now adds static import and export ... from sources to imports, next to dynamic import() (and module.register), which are already resolved with the import condition.
  • deps now only holds require-like references (require(), createRequire, require.resolve, special cases, wildcard requires), so emitDependency always resolves it with the require condition.

Relative and absolute paths resolve the same way under both conditions, so this only changes bare and # specifiers reached through require() in an ES module.

Tests

New unit fixture esm-require-exports-conditions: an ES module imports dual/imported and requires dual/required through createRequire. Each subpath has separate import and require targets.

  • Before: traces esm/imported.js and esm/required.js (the test fails).
  • After: traces esm/imported.js and cjs/required.js.

The repro from the issue (createRequire + require("dual/sub")), with mixedModules both true and false:

before: node_modules/dual/esm/sub.js node_modules/dual/package.json
after:  node_modules/dual/cjs/sub.js node_modules/dual/package.json

pnpm prettier-check, pnpm build and pnpm test pass locally. The exceptions are the bcrypt, ffmpeg-static and leveldown integration tests: they fail the same way on main here because I installed without running native build scripts.

@RClaudiuM
RClaudiuM requested review from a team, icyJoseph, ijjk and styfle as code owners November 7, 2026 21:28

@agammann agammann 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.

This also moves export ... from and export * from sources into imports, but the new dual-package fixture covers only a static import and createRequire. Could it include a re-export whose import and require targets differ and assert the import target is traced? That would cover the second AST path changed here.

@RClaudiuM

Copy link
Copy Markdown
Contributor Author

@agammann good point, added in 8874aa9. The fixture now also has export { format as reexportedFormat } from 'dual/reexported' and export * from 'dual/reexported-all', both pointing at subpaths whose import and require targets differ, and output.js expects the esm/ files for both. With the ExportNamedDeclaration/ExportAllDeclaration line reverted to deps.add, the test traces cjs/reexported.js and cjs/reexported-all.js instead and fails.

@agammann

Copy link
Copy Markdown

Thanks for the update. I rechecked head 8874aa9: the new fixture covers both named and wildcard re-exports with separate import and require targets. That addresses the test coverage I requested. I reviewed the diff but did not run the tests locally. AI-assisted review.

@styfle styfle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two non-blocking notes:

Non-blocking: The unit harness runs fixtures with mixedModules: true unless overridden (test/unit.test.js:196), so this fixture doesn't run in the default configuration from #621: under the harness the require() call is picked up by the mixed-modules branch rather than the pure-ESM createRequire (BOUND_REQUIRE) path, and the existing module-create-require-no-mixed fixture only requires a relative specifier, which never consults conditions. A test-opts.json with { "mixedModules": false } here (or a no-mixed sibling fixture) would pin the issue's own path — the traced output is the same either way.

Non-blocking: The new AnalyzeResult comments read as a strict invariant, but deps also receives glob results from dynamic import() wildcards (emitWildcardRequire ignores processRequireArg's isImport flag — harmless today since path specifiers never consult conditions), and imports also receives module.register() sources. Worth threading isImport through emitWildcardRequire or softening the wording.

Reviewed via styfle's devbox

@RClaudiuM

Copy link
Copy Markdown
Contributor Author

Thanks @styfle. Both notes are addressed in 507d6bd:

  • The fixture now has a test-opts.json with { "mixedModules": false }, so the require() goes through the createRequire path from the issue. Without the fix it traces dual/esm/required.js instead of dual/cjs/required.js, so it fails on main.
  • I softened the AnalyzeResult comments instead of changing behaviour. deps is noted as also receiving the files matched by wildcard require() and import() expressions, which are paths, so conditions do not apply. imports is noted as also receiving module.register() sources. Threading isImport through emitWildcardRequire seemed better as a separate change.

1484 tests pass.

@styfle

styfle commented Nov 17, 2026

Copy link
Copy Markdown
Member

Commits must have verified signatures.

You'll need to sign your commits and force push

A require() call in an ES module (through module.createRequire, or the
global require with mixedModules) was resolved with the "import" condition,
because every specifier in `deps` was resolved as an import when the
containing file was ESM. For a dual package whose exports subpath has
separate import and require targets, the ESM build was traced while Node
loads the CommonJS one.

Static import and export-from declarations now go into `imports`, next to
dynamic import(), and `deps` only holds require-like references, which are
always resolved with the "require" condition.

Fixes vercel#621
Without mixedModules the require() in the fixture goes through the
module.createRequire path from the issue. Also note in the AnalyzeResult
comments that deps receives wildcard import() matches and imports
receives module.register() sources.
@RClaudiuM
RClaudiuM force-pushed the fix/621-require-in-esm-cjs-resolve branch from 507d6bd to aff6814 Compare November 17, 2026 19:06

@styfle styfle left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice work — thanks for addressing the follow-ups!

Reviewed via styfle's devbox

@styfle
styfle enabled auto-merge (squash) November 19, 2026 20:19
@styfle
styfle merged commit 648c381 into vercel:main Nov 19, 2026
17 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 1.11.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

require() inside an ES module is resolved with the import condition, tracing the wrong file of a dual package

3 participants