Skip to content

Conversation

Momena-akhtar
Copy link

Description

This PR adds an example to the doc/api/esm.md documentation demonstrating how to simulate runtime-specific conditional exports (such as "electron") using the --import flag.

Since Node.js does not currently support dynamically registering custom conditions for "exports" resolution, this guide shows a practical workaround that leverages environment variables and preload scripts.

The example illustrates:

  • Creating a preload script to detect the runtime (e.g., Electron)
  • Conditionally exporting different modules at runtime
  • Limitations and use cases

This replaces the previous proposal to add a registerCondition() API by showing a userland solution that does not require changes to Node.js core.

✳️ Moved from the previous location (doc/contributing/) to the API documentation under doc/api/esm.md as recommended in #59066

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/loaders

@nodejs-github-bot nodejs-github-bot added doc Issues and PRs related to the documentations. esm Issues and PRs related to the ECMAScript Modules implementation. labels Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants