diff --git a/doc/api/esm.md b/doc/api/esm.md index e4a865752d7ee7..f53d5561888302 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -199,7 +199,13 @@ import _ from 'data:application/json,"world!"'; #### `node:` Imports `node:` URLs are supported as an alternative means to load Node.js builtin diff --git a/doc/api/modules.md b/doc/api/modules.md index 1667c149953773..b17bd9eb390538 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -280,6 +280,12 @@ irrespective of whether or not `./foo` and `./FOO` are the same file. ## Core modules + Node.js has several modules compiled into the binary. These modules are described in greater detail elsewhere in this documentation. @@ -291,6 +297,11 @@ Core modules are always preferentially loaded if their identifier is passed to `require()`. For instance, `require('http')` will always return the built in HTTP module, even if there is a file by that name. +Core modules can also be identified using the `node:` prefix, in which case +it bypasses the `require` cache. For instance, `require('node:http')` will +always return the built in HTTP module, even if there is `require.cache` entry +by that name. + ## Cycles @@ -642,8 +653,19 @@ error. Adding or replacing entries is also possible. This cache is checked before native modules and if a name matching a native module is added to the cache, -no require call is -going to receive the native module anymore. Use with care! +only `node:`-prefixed require calls are going to receive the native module. +Use with care! + +```js +const assert = require('assert'); +const realFs = require('fs'); + +const fakeFs = {}; +require.cache.fs = { exports: fakeFs }; + +assert.strictEqual(require('fs'), fakeFs); +assert.strictEqual(require('node:fs'), realFs); +``` #### `require.extensions`