Skip to content

doc: guide for runtime-specific conditional exports using --import #59075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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