Skip to content

Commit 214cf17

Browse files
aduh95danielleadams
authored andcommitted
doc: recommend package exports instead of requiring folders
PR-URL: #41381 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 5c387a0 commit 214cf17

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

doc/api/modules.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,8 @@ If the given path does not exist, `require()` will throw an [`Error`][] with its
434434

435435
<!--type=misc-->
436436

437-
It is convenient to organize programs and libraries into self-contained
438-
directories, and then provide a single entry point to those directories.
437+
> Stability: 3 - Legacy: Use [subpath exports][] or [subpath imports][] instead.
438+
439439
There are three ways in which a folder may be passed to `require()` as
440440
an argument.
441441

@@ -452,8 +452,6 @@ If this was in a folder at `./some-library`, then
452452
`require('./some-library')` would attempt to load
453453
`./some-library/lib/some-library.js`.
454454

455-
This is the extent of the awareness of `package.json` files within Node.js.
456-
457455
If there is no [`package.json`][] file present in the directory, or if the
458456
[`"main"`][] entry is missing or cannot be resolved, then Node.js
459457
will attempt to load an `index.js` or `index.node` file out of that
@@ -470,6 +468,11 @@ with the default error:
470468
Error: Cannot find module 'some-library'
471469
```
472470

471+
In all three above cases, an `import('./some-library')` call would result in a
472+
[`ERR_UNSUPPORTED_DIR_IMPORT`][] error. Using package [subpath exports][] or
473+
[subpath imports][] can provide the same containment organization benefits as
474+
folders as modules, and work for both `require` and `import`.
475+
473476
## Loading from `node_modules` folders
474477

475478
<!--type=misc-->
@@ -1076,6 +1079,7 @@ This section was moved to
10761079
[`"main"`]: packages.md#main
10771080
[`"type"`]: packages.md#type
10781081
[`ERR_REQUIRE_ESM`]: errors.md#err_require_esm
1082+
[`ERR_UNSUPPORTED_DIR_IMPORT`]: errors.md#err_unsupported_dir_import
10791083
[`Error`]: errors.md#class-error
10801084
[`__dirname`]: #__dirname
10811085
[`__filename`]: #__filename
@@ -1090,3 +1094,5 @@ This section was moved to
10901094
[exports shortcut]: #exports-shortcut
10911095
[module resolution]: #all-together
10921096
[native addons]: addons.md
1097+
[subpath exports]: packages.md#subpath-exports
1098+
[subpath imports]: packages.md#subpath-imports

0 commit comments

Comments
 (0)