Skip to content

Commit 133b10a

Browse files
0hmXaduh95
authored andcommitted
doc: add path rules and validation for export targets in package.json
PR-URL: #58604 Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 3ac8c68 commit 133b10a

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

doc/api/packages.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,59 @@ subpaths where possible instead of a separate map entry per package subpath
444444
export. This also mirrors the requirement of using [the full specifier path][]
445445
in relative and absolute import specifiers.
446446

447+
#### Path Rules and Validation for Export Targets
448+
449+
When defining paths as targets in the [`"exports"`][] field, Node.js enforces
450+
several rules to ensure security, predictability, and proper encapsulation.
451+
Understanding these rules is crucial for authors publishing packages.
452+
453+
##### Targets must be relative URLs
454+
455+
All target paths in the [`"exports"`][] map (the values associated with export
456+
keys) must be relative URL strings starting with `./`.
457+
458+
```json
459+
// package.json
460+
{
461+
"name": "my-package",
462+
"exports": {
463+
".": "./dist/main.js", // Correct
464+
"./feature": "./lib/feature.js", // Correct
465+
// "./origin-relative": "/dist/main.js", // Incorrect: Must start with ./
466+
// "./absolute": "file:///dev/null", // Incorrect: Must start with ./
467+
// "./outside": "../common/util.js" // Incorrect: Must start with ./
468+
}
469+
}
470+
```
471+
472+
Reasons for this behavior include:
473+
474+
* **Security:** Prevents exporting arbitrary files from outside the
475+
package's own directory.
476+
* **Encapsulation:** Ensures all exported paths are resolved relative to
477+
the package root, making the package self-contained.
478+
479+
##### No path traversal or invalid segments
480+
481+
Export targets must not resolve to a location outside the package's root
482+
directory. Additionally, path segments like `.` (single dot), `..` (double dot),
483+
or `node_modules` (and their URL-encoded equivalents) are generally disallowed
484+
within the `target` string after the initial `./` and in any `subpath` part
485+
substituted into a target pattern.
486+
487+
```json
488+
// package.json
489+
{
490+
"name": "my-package",
491+
"exports": {
492+
// ".": "./dist/../../elsewhere/file.js", // Invalid: path traversal
493+
// ".": "././dist/main.js", // Invalid: contains "." segment
494+
// ".": "./dist/../dist/main.js", // Invalid: contains ".." segment
495+
// "./utils/./helper.js": "./utils/helper.js" // Key has invalid segment
496+
}
497+
}
498+
```
499+
447500
### Exports sugar
448501

449502
<!-- YAML

0 commit comments

Comments
 (0)