Skip to content

feat: add ESM support via Node.js loader hooks#148

Merged
ilearnio merged 1 commit intoilearnio:masterfrom
artnikbrothers:artur.havrylov/module-alias/feature/esm-support
Feb 2, 2026
Merged

feat: add ESM support via Node.js loader hooks#148
ilearnio merged 1 commit intoilearnio:masterfrom
artnikbrothers:artur.havrylov/module-alias/feature/esm-support

Conversation

@artnikbrothers
Copy link
Copy Markdown
Contributor

Summary

  • Add native ES module support using Node.js loader hooks API
  • Support _moduleAliases and _moduleDirectories from package.json in ESM
  • Automatic version detection: registerHooks() for Node 22.15+, register() for Node 18.19+
  • Full backward compatibility with existing CJS usage

Usage

node --import module-alias/register ./app.mjs

Changes

  • register.mjs - ESM entry point with version detection
  • esm-loader.mjs - Core resolve hooks implementation
  • package.json - Added exports field for dual CJS/ESM support
  • README.md - ESM documentation section
  • .github/workflows/node.js.yml - CI for Node 18/20/22

Test Plan

  • Unit tests for isPathMatchesAlias and resolveAlias (11 tests)
  • Integration tests spawning node with --import flag (2 tests)
  • Tested on Node 18, 20, 22, 24
  • CJS backward compatibility verified (20 existing tests pass)
  • POC app tested with _moduleAliases and _moduleDirectories

Closes #59
Closes #113

- Add register.mjs as ESM entry point for --import flag
- Add esm-loader.mjs with resolve hooks for _moduleAliases and _moduleDirectories
- Support Node 22.15+ (registerHooks) and Node 18.19+ (register) APIs
- Add exports field to package.json for dual CJS/ESM support
- Add ESM test suite (13 tests) with fixtures
- Update CI to test Node 18/20/22 with ESM
- Update README with ESM usage documentation

Closes ilearnio#59
Closes ilearnio#113
@ilearnio ilearnio merged commit 488ef40 into ilearnio:master Feb 2, 2026
@ilearnio
Copy link
Copy Markdown
Owner

ilearnio commented Feb 2, 2026

Great job! This is very much needed 🔥

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