Skip to content
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

🚀 Feature: Recommend main and exports #69

Open
brettz9 opened this issue Apr 30, 2020 · 1 comment
Open

🚀 Feature: Recommend main and exports #69

brettz9 opened this issue Apr 30, 2020 · 1 comment
Labels
status: accepting prs Please, send a pull request to resolve this! 🙏 type: feature New enhancement or request 🚀

Comments

@brettz9
Copy link

brettz9 commented Apr 30, 2020

Per https://nodejs.org/api/esm.html#esm_main_entry_point_export

To set the main entry point for a package, it is advisable to define both "exports" and "main" in the package’s package.json file:

{
"main": "./main.js",
"exports": "./main.js"
}

The benefit of doing this is that when using the "exports" field all subpaths of the package will no longer be available to importers under require('pkg/subpath.js'), and instead they will get a new error, ERR_PACKAGE_PATH_NOT_EXPORTED.

This encapsulation of exports provides more reliable guarantees about package interfaces for tools and when handling semver upgrades for a package. It is not a strong encapsulation since a direct require of any absolute subpath of the package such as require('/path/to/node_modules/pkg/subpath.js') will still load subpath.js.

Also the order within exports (and the order within exports subpaths, e.g., {exports: {"./feature": {browser: "./feature-browser.js", default: "./feature.js"}} and within nested conditions, e.g., {"exports": {"import": {"node": "./feature-node.mjs"}}}) could be enforced per https://nodejs.org/api/esm.html#esm_conditional_exports :

"node" - matched for any Node.js environment. Can be a CommonJS or ES module file. This condition should always come after "import" or "require".
"default" - the generic fallback that will always match. Can be a CommonJS or ES module file. This condition should always come last.

@LinusU
Copy link
Collaborator

LinusU commented Apr 30, 2020

We should probably note that it's in most cases a breaking change to add exports:

https://medium.com/javascript-in-plain-english/is-promise-post-mortem-cab807f18dcc

@JoshuaKGoldberg JoshuaKGoldberg added type: feature New enhancement or request 🚀 status: accepting prs Please, send a pull request to resolve this! 🙏 labels Mar 28, 2024
@JoshuaKGoldberg JoshuaKGoldberg changed the title Recommend main and exports 🚀 Feature: Recommend main and exports Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: accepting prs Please, send a pull request to resolve this! 🙏 type: feature New enhancement or request 🚀
Projects
None yet
Development

No branches or pull requests

3 participants