diff --git a/doc/api/esm.md b/doc/api/esm.md index c9635c4a8a77cb..bd6fe5e6dde4b9 100644 --- a/doc/api/esm.md +++ b/doc/api/esm.md @@ -204,6 +204,10 @@ import _ from 'data:application/json,"world!"'; added: - v14.13.1 - v12.20.0 +changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/37246 + description: Added `node:` import support to `require(...)`. --> `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 48eb28fd59b96c..611deba2b73fbb 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`