@@ -444,6 +444,59 @@ subpaths where possible instead of a separate map entry per package subpath
444
444
export. This also mirrors the requirement of using [ the full specifier path] [ ]
445
445
in relative and absolute import specifiers.
446
446
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
+
447
500
### Exports sugar
448
501
449
502
<!-- YAML
0 commit comments