Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
module: add support for
node:‑prefixed require(…) calls
Fixes: #36098 Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com> Co-authored-by: Guy Bedford <guybedford@gmail.com> Co-authored-by: Darshan Sen <raisinten@gmail.com> PR-URL: #37246 Reviewed-By: Bradley Farias <bradley.meck@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Darshan Sen <raisinten@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
- Loading branch information
Showing
with
90 additions
and 6 deletions.
| @@ -0,0 +1,42 @@ | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const fs = require('fs'); | ||
|
|
||
| const errUnknownBuiltinModuleRE = /^No such built-in module: /u; | ||
|
|
||
| // For direct use of require expressions inside of CJS modules, | ||
| // all kinds of specifiers should work without issue. | ||
| { | ||
| assert.strictEqual(require('fs'), fs); | ||
| assert.strictEqual(require('node:fs'), fs); | ||
|
|
||
| assert.throws( | ||
| () => require('node:unknown'), | ||
| { | ||
| code: 'ERR_UNKNOWN_BUILTIN_MODULE', | ||
| message: errUnknownBuiltinModuleRE, | ||
| }, | ||
| ); | ||
|
|
||
| assert.throws( | ||
| () => require('node:internal/test/binding'), | ||
| { | ||
| code: 'ERR_UNKNOWN_BUILTIN_MODULE', | ||
| message: errUnknownBuiltinModuleRE, | ||
| }, | ||
| ); | ||
| } | ||
|
|
||
| // `node:`-prefixed `require(...)` calls bypass the require cache: | ||
| { | ||
| const fakeModule = {}; | ||
|
|
||
| require.cache.fs = { exports: fakeModule }; | ||
|
|
||
| assert.strictEqual(require('fs'), fakeModule); | ||
| assert.strictEqual(require('node:fs'), fs); | ||
|
|
||
| delete require.cache.fs; | ||
| } |

