Skip to content

Commit 91b820e

Browse files
guybedforddanielleadams
authored andcommitted
esm: use "node:" namespace for builtins
PR-URL: #35387 Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Jan Krems <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent af360ac commit 91b820e

10 files changed

+19
-19
lines changed

doc/api/esm.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ The resolver can throw the following errors:
10031003
> 1. If _selfUrl_ is not **undefined**, return _selfUrl_.
10041004
> 1. If _packageSubpath_ is _"."_ and _packageName_ is a Node.js builtin
10051005
> module, then
1006-
> 1. Return the string _"nodejs:"_ concatenated with _packageSpecifier_.
1006+
> 1. Return the string _"node:"_ concatenated with _packageSpecifier_.
10071007
> 1. While _parentURL_ is not the file system root,
10081008
> 1. Let _packageURL_ be the URL resolution of _"node_modules/"_
10091009
> concatenated with _packageSpecifier_, relative to _parentURL_.

lib/internal/modules/esm/get_format.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ if (experimentalJsonModules)
3434
extensionFormatMap['.json'] = legacyExtensionFormatMap['.json'] = 'json';
3535

3636
function defaultGetFormat(url, context, defaultGetFormatUnused) {
37-
if (StringPrototypeStartsWith(url, 'nodejs:')) {
37+
if (StringPrototypeStartsWith(url, 'node:')) {
3838
return { format: 'builtin' };
3939
}
4040
const parsed = new URL(url);

lib/internal/modules/esm/resolve.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -776,13 +776,13 @@ function defaultResolve(specifier, context = {}, defaultResolveUnused) {
776776
};
777777
}
778778
} catch {}
779-
if (parsed && parsed.protocol === 'nodejs:')
779+
if (parsed && parsed.protocol === 'node:')
780780
return { url: specifier };
781781
if (parsed && parsed.protocol !== 'file:' && parsed.protocol !== 'data:')
782782
throw new ERR_UNSUPPORTED_ESM_URL_SCHEME(parsed);
783783
if (NativeModule.canBeRequiredByUsers(specifier)) {
784784
return {
785-
url: 'nodejs:' + specifier
785+
url: 'node:' + specifier
786786
};
787787
}
788788
if (parentURL && StringPrototypeStartsWith(parentURL, 'data:')) {

lib/internal/modules/esm/translators.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ function cjsPreparseModuleExports(filename) {
223223
// through normal resolution
224224
translators.set('builtin', async function builtinStrategy(url) {
225225
debug(`Translating BuiltinModule ${url}`);
226-
// Slice 'nodejs:' scheme
227-
const id = url.slice(7);
226+
// Slice 'node:' scheme
227+
const id = url.slice(5);
228228
const module = loadNativeModule(id, url, true);
229-
if (!url.startsWith('nodejs:') || !module) {
230-
throw new ERR_UNKNOWN_BUILTIN_MODULE(id);
229+
if (!url.startsWith('node:') || !module) {
230+
throw new ERR_UNKNOWN_BUILTIN_MODULE(url);
231231
}
232232
debug(`Loading BuiltinModule ${url}`);
233233
return module.getESMFacade();

test/es-module/test-esm-dynamic-import.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ function expectFsNamespace(result) {
4848
expectFsNamespace(import('fs'));
4949
expectFsNamespace(eval('import("fs")'));
5050
expectFsNamespace(eval('import("fs")'));
51-
expectFsNamespace(import('nodejs:fs'));
51+
expectFsNamespace(import('node:fs'));
5252

53-
expectModuleError(import('nodejs:unknown'),
53+
expectModuleError(import('node:unknown'),
5454
'ERR_UNKNOWN_BUILTIN_MODULE');
5555
expectModuleError(import('./not-an-existing-module.mjs'),
5656
'ERR_MODULE_NOT_FOUND');

test/fixtures/es-module-loaders/builtin-named-exports-loader.mjs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export function getGlobalPreloadCode() {
1515

1616
export function resolve(specifier, context, defaultResolve) {
1717
const def = defaultResolve(specifier, context);
18-
if (def.url.startsWith('nodejs:')) {
18+
if (def.url.startsWith('node:')) {
1919
return {
2020
url: `custom-${def.url}`,
2121
};
@@ -24,7 +24,7 @@ export function resolve(specifier, context, defaultResolve) {
2424
}
2525

2626
export function getSource(url, context, defaultGetSource) {
27-
if (url.startsWith('custom-nodejs:')) {
27+
if (url.startsWith('custom-node:')) {
2828
const urlObj = new URL(url);
2929
return {
3030
source: generateBuiltinModule(urlObj.pathname),
@@ -35,7 +35,7 @@ export function getSource(url, context, defaultGetSource) {
3535
}
3636

3737
export function getFormat(url, context, defaultGetFormat) {
38-
if (url.startsWith('custom-nodejs:')) {
38+
if (url.startsWith('custom-node:')) {
3939
return { format: 'module' };
4040
}
4141
return defaultGetFormat(url, context, defaultGetFormat);

test/fixtures/es-module-loaders/example-loader.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ baseURL.pathname = process.cwd() + '/';
1111
export function resolve(specifier, { parentURL = baseURL }, defaultResolve) {
1212
if (builtinModules.includes(specifier)) {
1313
return {
14-
url: 'nodejs:' + specifier
14+
url: 'node:' + specifier
1515
};
1616
}
1717
if (/^\.{1,2}[/]/.test(specifier) !== true && !specifier.startsWith('file:')) {
@@ -27,7 +27,7 @@ export function resolve(specifier, { parentURL = baseURL }, defaultResolve) {
2727
}
2828

2929
export function getFormat(url, context, defaultGetFormat) {
30-
if (url.startsWith('nodejs:') && builtinModules.includes(url.slice(7))) {
30+
if (url.startsWith('node:') && builtinModules.includes(url.slice(5))) {
3131
return {
3232
format: 'builtin'
3333
};

test/fixtures/es-module-loaders/loader-unknown-builtin-module.mjs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
export async function resolve(specifier, { parentURL }, defaultResolve) {
22
if (specifier === 'unknown-builtin-module') {
33
return {
4-
url: 'nodejs:unknown-builtin-module'
4+
url: 'node:unknown-builtin-module'
55
};
66
}
77
return defaultResolve(specifier, {parentURL}, defaultResolve);
88
}
99

1010
export async function getFormat(url, context, defaultGetFormat) {
11-
if (url === 'nodejs:unknown-builtin-module') {
11+
if (url === 'node:unknown-builtin-module') {
1212
return {
1313
format: 'builtin'
1414
};

test/fixtures/es-module-loaders/not-found-assert-loader.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function resolve(specifier, { parentURL }, defaultResolve) {
1414
catch (e) {
1515
assert.strictEqual(e.code, 'ERR_MODULE_NOT_FOUND');
1616
return {
17-
url: 'nodejs:fs'
17+
url: 'node:fs'
1818
};
1919
}
2020
assert.fail(`Module resolution for ${specifier} should be throw ERR_MODULE_NOT_FOUND`);

test/parallel/test-loaders-unknown-builtin-module.mjs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { expectsError, mustCall } from '../common/index.mjs';
33
import assert from 'assert';
44

5-
const unknownBuiltinModule = 'unknown-builtin-module';
5+
const unknownBuiltinModule = 'node:unknown-builtin-module';
66

77
import(unknownBuiltinModule)
88
.then(assert.fail, expectsError({

0 commit comments

Comments
 (0)