Skip to content

Fix absolute require for ESM modules#1470

Merged
ricardo-devis-agullo merged 1 commit into
masterfrom
fix-abs-require
May 11, 2026
Merged

Fix absolute require for ESM modules#1470
ricardo-devis-agullo merged 1 commit into
masterfrom
fix-abs-require

Conversation

@ricardo-devis-agullo
Copy link
Copy Markdown
Collaborator

Summary

This PR fixes two related issues in the registry's require-wrapper:

  1. ESM-only modules failing to load — Some ESM-only packages do not define a main field in their package.json, causing tryRequire with an absolute path to fail. The wrapper now falls back to resolving by package name when the absolute path fails.
  2. Core module resolution priority — Core modules (e.g., url, path) are now checked before looking up userland packages. This prevents a malicious or conflicting npm package from shadowing a built-in module.

Changes

  • src/registry/domain/require-wrapper.ts

    • Added tryRequire(requirePath) fallback for ESM-only modules without a main field
    • Reordered resolution logic to prioritize core dependencies over npm packages
    • Fixed minor linting issue (arrow function parentheses)
  • test/unit/registry-domain-require-wrapper.js

    • Added test for node: prefixed core modules
    • Added test verifying fallback from absolute path to package name for ESM modules
    • Added test ensuring core modules take precedence over npm packages with the same name

Testing

  • All 9 registry-domain-require-wrapper unit tests pass
  • Full build passes (npm run build)

@ricardo-devis-agullo ricardo-devis-agullo merged commit 669b80d into master May 11, 2026
3 checks passed
@ricardo-devis-agullo ricardo-devis-agullo deleted the fix-abs-require branch May 11, 2026 10:32
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.

1 participant