Skip to content

Commit 6eae414

Browse files
committed
doc: add information about modules cache behavior
This publicly documents that adding native module names will resolve the added entry instead of the native module. It also updates the description why extensions are deprecated. PR-URL: nodejs#26971 Refs: nodejs#25362 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com>
1 parent fcdee74 commit 6eae414

File tree

1 file changed

+25
-31
lines changed

1 file changed

+25
-31
lines changed

doc/api/modules.md

+25-31
Original file line numberDiff line numberDiff line change
@@ -199,28 +199,26 @@ NODE_MODULES_PATHS(START)
199199

200200
<!--type=misc-->
201201

202-
Modules are cached after the first time they are loaded. This means
203-
(among other things) that every call to `require('foo')` will get
204-
exactly the same object returned, if it would resolve to the same file.
202+
Modules are cached after the first time they are loaded. This means (among other
203+
things) that every call to `require('foo')` will get exactly the same object
204+
returned, if it would resolve to the same file.
205205

206-
Provided `require.cache` is not modified, multiple calls to
207-
`require('foo')` will not cause the module code to be executed multiple times.
208-
This is an important feature. With it, "partially done" objects can be returned,
209-
thus allowing transitive dependencies to be loaded even when they would cause
210-
cycles.
206+
Provided `require.cache` is not modified, multiple calls to `require('foo')`
207+
will not cause the module code to be executed multiple times. This is an
208+
important feature. With it, "partially done" objects can be returned, thus
209+
allowing transitive dependencies to be loaded even when they would cause cycles.
211210

212-
To have a module execute code multiple times, export a function, and call
213-
that function.
211+
To have a module execute code multiple times, export a function, and call that
212+
function.
214213

215214
### Module Caching Caveats
216215

217216
<!--type=misc-->
218217

219-
Modules are cached based on their resolved filename. Since modules may
220-
resolve to a different filename based on the location of the calling
221-
module (loading from `node_modules` folders), it is not a *guarantee*
222-
that `require('foo')` will always return the exact same object, if it
223-
would resolve to different files.
218+
Modules are cached based on their resolved filename. Since modules may resolve
219+
to a different filename based on the location of the calling module (loading
220+
from `node_modules` folders), it is not a *guarantee* that `require('foo')` will
221+
always return the exact same object, if it would resolve to different files.
224222

225223
Additionally, on case-insensitive file systems or operating systems, different
226224
resolved filenames can point to the same file, but the cache will still treat
@@ -415,7 +413,7 @@ are not found elsewhere.
415413
On Windows, `NODE_PATH` is delimited by semicolons (`;`) instead of colons.
416414

417415
`NODE_PATH` was originally created to support loading modules from
418-
varying paths before the current [module resolution][] algorithm was frozen.
416+
varying paths before the current [module resolution][] algorithm was defined.
419417

420418
`NODE_PATH` is still supported, but is less necessary now that the Node.js
421419
ecosystem has settled on a convention for locating dependent modules.
@@ -588,6 +586,11 @@ value from this object, the next `require` will reload the module. Note that
588586
this does not apply to [native addons][], for which reloading will result in an
589587
error.
590588

589+
Adding or replacing entries is also possible. This cache is checked before
590+
native modules and if a name matching a native module is added to the cache,
591+
no require call is
592+
going to receive the native module anymore. Use with care!
593+
591594
#### require.extensions
592595
<!-- YAML
593596
added: v0.3.0
@@ -606,22 +609,13 @@ Process files with the extension `.sjs` as `.js`:
606609
require.extensions['.sjs'] = require.extensions['.js'];
607610
```
608611

609-
**Deprecated.** In the past, this list has been used to load
610-
non-JavaScript modules into Node.js by compiling them on-demand.
611-
However, in practice, there are much better ways to do this, such as
612-
loading modules via some other Node.js program, or compiling them to
613-
JavaScript ahead of time.
614-
615-
Since the module system is locked, this feature will probably never go
616-
away. However, it may have subtle bugs and complexities that are best
617-
left untouched.
618-
619-
Note that the number of file system operations that the module system
620-
has to perform in order to resolve a `require(...)` statement to a
621-
filename scales linearly with the number of registered extensions.
612+
**Deprecated.** In the past, this list has been used to load non-JavaScript
613+
modules into Node.js by compiling them on-demand. However, in practice, there
614+
are much better ways to do this, such as loading modules via some other Node.js
615+
program, or compiling them to JavaScript ahead of time.
622616

623-
In other words, adding extensions slows down the module loader and
624-
should be discouraged.
617+
Avoid using `require.extensions`. Use could cause subtle bugs and resolving the
618+
extensions gets slower with each registered extension.
625619

626620
#### require.main
627621
<!-- YAML

0 commit comments

Comments
 (0)