exports-subpaths-style
💼 This rule is enabled in the 🎨 stylistic config.
🔧 This rule is automatically fixable by the --fix CLI option.
This rule enforces consistent formatting for the exports field in package.json, ensuring either explicit subpath notation with "." or implicit root-level exports.
Rule Details
Section titled “Rule Details”The Node.js exports field supports two equivalent formats for defining a single root export:
Explicit format (with "." subpath):
{ "exports": { ".": "./index.js" }}Implicit format (root-level value):
{ "exports": "./index.js"}Both formats are functionally equivalent for a single root export, but using one consistently improves readability and maintainability across packages.
💡 This rule is especially useful in monorepos where consistency across multiple
package.jsonfiles is important.
Examples with Default Options (prefer: “explicit”)
Section titled “Examples with Default Options (prefer: “explicit”)”Examples of incorrect code with default options:
{ "exports": "./index.js"}{ "exports": { "import": "./index.mjs", "require": "./index.cjs" }}Examples of correct code with default options:
{ "exports": { ".": "./index.js" }}{ "exports": { ".": { "import": "./index.mjs", "require": "./index.cjs" } }}{ "exports": { ".": "./index.js", "./utils": "./utils.js" }}Examples with prefer: “implicit”
Section titled “Examples with prefer: “implicit””Examples of incorrect code with { "prefer": "implicit" }:
{ "exports": { ".": "./index.js" }}{ "exports": { ".": { "import": "./index.mjs", "require": "./index.cjs" } }}Examples of correct code with { "prefer": "implicit" }:
{ "exports": "./index.js"}{ "exports": { "import": "./index.mjs", "require": "./index.cjs" }}{ "exports": { ".": "./index.js", "./utils": "./utils.js" }}Note: Multiple subpaths always require the explicit object format and are not transformed by this rule.
Options
Section titled “Options”| Name | Description | Type | Choices | Default |
|---|---|---|---|---|
prefer | Specifies which exports format to enforce. | String | implicit, explicit | explicit |
prefer
Section titled “prefer”The prefer option specifies which format to enforce:
"explicit"(default): Requires the"."subpath key for single root exports"implicit": Prefers the shorthand format without"."for single root exports
{ "package-json/exports-subpaths-style": [ "error", { "prefer": "explicit" } ]}Default: "explicit"
ℹ️ The explicit format is generally recommended as it’s more consistent when packages later add additional subpaths.
This rule is autofixable; run eslint with the --fix option to automatically convert between formats.
When Not to Use It
Section titled “When Not to Use It”If your project doesn’t use the exports field or you don’t have a preference for consistency in exports formatting, you can disable this rule.