Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6370b4d

Browse filesBrowse files
aduh95BethGriggs
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 b5104e8 commit 6370b4d
Copy full SHA for 6370b4d

File tree

Expand file treeCollapse file tree

1 file changed

+10
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+10
-4
lines changed
Open diff view settings
Collapse file

‎doc/api/modules.md‎

Copy file name to clipboardExpand all lines: doc/api/modules.md
+10-4Lines changed: 10 additions & 4 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,8 @@ If the given path does not exist, `require()` will throw an [`Error`][] with its
436436

437437
<!--type=misc-->
438438

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

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

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

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

477480
<!--type=misc-->
@@ -1080,6 +1083,7 @@ This section was moved to
10801083
[`"main"`]: packages.md#main
10811084
[`"type"`]: packages.md#type
10821085
[`ERR_REQUIRE_ESM`]: errors.md#err_require_esm
1086+
[`ERR_UNSUPPORTED_DIR_IMPORT`]: errors.md#err_unsupported_dir_import
10831087
[`Error`]: errors.md#class-error
10841088
[`__dirname`]: #__dirname
10851089
[`__filename`]: #__filename
@@ -1094,3 +1098,5 @@ This section was moved to
10941098
[exports shortcut]: #exports-shortcut
10951099
[module resolution]: #all-together
10961100
[native addons]: addons.md
1101+
[subpath exports]: packages.md#subpath-exports
1102+
[subpath imports]: packages.md#subpath-imports

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.