Skip to content

Commit 33a7c62

Browse files
committed
module: emit experimental warning for module.register()
1 parent 7ffbb76 commit 33a7c62

File tree

4 files changed

+65
-1
lines changed

4 files changed

+65
-1
lines changed

doc/api/module.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ added:
179179
- v20.6.0
180180
- v18.19.0
181181
changes:
182+
- version: REPLACEME
183+
pr-url: https://github.com/nodejs/node/pull/REPLACEME
184+
description: Runtime experimental warning added.
182185
- version:
183186
- v23.6.1
184187
- v22.13.1

lib/internal/modules/esm/loader.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ const {
3131
} = require('internal/errors').codes;
3232
const { getOptionValue } = require('internal/options');
3333
const { isURL, pathToFileURL } = require('internal/url');
34-
const { kEmptyObject } = require('internal/util');
34+
const {
35+
getDeprecationWarningEmitter,
36+
kEmptyObject,
37+
} = require('internal/util');
3538
const {
3639
compileSourceTextModule,
3740
SourceTextModuleTypes: { kUser },
@@ -955,7 +958,16 @@ function isCascadedLoaderInitialized() {
955958
* });
956959
* ```
957960
*/
961+
const emitRegisterExperimentalWarning = getDeprecationWarningEmitter(
962+
'ExperimentalWarning',
963+
'`module.register()` is an experimental feature and will be removed in a future version of Node.js. ' +
964+
'Use `module.registerHooks()` instead.',
965+
undefined,
966+
false,
967+
);
968+
958969
function register(specifier, parentURL = undefined, options) {
970+
emitRegisterExperimentalWarning();
959971
if (parentURL != null && typeof parentURL === 'object' && !isURL(parentURL)) {
960972
options = parentURL;
961973
parentURL = options.parentURL;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import { spawnPromisified } from '../common/index.mjs';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
4+
import assert from 'node:assert';
5+
import { execPath } from 'node:process';
6+
import { describe, it } from 'node:test';
7+
8+
const urlToRegister = fixtures.fileURL('es-module-loaders', 'loader-resolve-passthru.mjs');
9+
const urlToRegisterEscaped = JSON.stringify(urlToRegister.href);
10+
11+
12+
describe('module.register() experimental warning', { concurrency: !process.env.TEST_PARALLEL }, () => {
13+
it('emits ExperimentalWarning when module.register() is called', async () => {
14+
const { code, stderr } = await spawnPromisified(execPath, [
15+
'--input-type=module',
16+
'--eval',
17+
`import { register } from 'node:module'; register(${urlToRegisterEscaped});`,
18+
]);
19+
20+
assert.ok(stderr.includes('ExperimentalWarning: `module.register()` is an experimental feature and will be removed in a future version of Node.js. Use `module.registerHooks()` instead.'));
21+
assert.strictEqual(code, 0);
22+
});
23+
24+
it('only emits the warning once per process', async () => {
25+
const { code, stderr } = await spawnPromisified(execPath, [
26+
'--input-type=module',
27+
'--eval',
28+
`import { register } from 'node:module';
29+
register(${urlToRegisterEscaped});
30+
register(${urlToRegisterEscaped});`,
31+
]);
32+
33+
assert.strictEqual(stderr.split('module.register()').length - 1, 1);
34+
assert.strictEqual(code, 0);
35+
});
36+
37+
it('does not emit when --no-warnings is set', async () => {
38+
const { code, stderr } = await spawnPromisified(execPath, [
39+
'--no-warnings',
40+
'--input-type=module',
41+
'--eval',
42+
`import { register } from 'node:module'; register(${urlToRegisterEscaped});`,
43+
]);
44+
45+
assert.doesNotMatch(stderr, /ExperimentalWarning/);
46+
assert.strictEqual(code, 0);
47+
});
48+
});

test/module-hooks/test-async-loader-hooks-use-hooks-require-esm.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { spawnSyncAndAssert } from '../common/child_process.js';
77
spawnSyncAndAssert(
88
execPath,
99
[
10+
'--no-warnings',
1011
'--no-experimental-require-module',
1112
'--import',
1213
fixtures.fileURL('es-module-loaders/builtin-named-exports.mjs'),

0 commit comments

Comments
 (0)