Skip to content

Commit c5ce15e

Browse files
committed
module: runtime-deprecate module.register() (DEP0205)
1 parent d153a03 commit c5ce15e

File tree

4 files changed

+79
-2
lines changed

4 files changed

+79
-2
lines changed

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4521,12 +4521,15 @@ deprecated and will throw an error in a future version.
45214521
45224522
<!-- YAML
45234523
changes:
4524+
- version: REPLACEME
4525+
pr-url: https://github.com/nodejs/node/pull/REPLACEME
4526+
description: Runtime deprecation.
45244527
- version: REPLACEME
45254528
pr-url: https://github.com/nodejs/node/pull/62395
45264529
description: Documentation-only deprecation.
45274530
-->
45284531
4529-
Type: Documentation-only
4532+
Type: Runtime
45304533
45314534
[`module.register()`][] is deprecated. Use [`module.registerHooks()`][]
45324535
instead.

doc/api/module.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ added:
180180
- v18.19.0
181181
deprecated: REPLACEME
182182
changes:
183+
- version: REPLACEME
184+
pr-url: https://github.com/nodejs/node/pull/REPLACEME
185+
description: Runtime deprecation (DEP0205).
183186
- version: REPLACEME
184187
pr-url: https://github.com/nodejs/node/pull/62395
185188
description: Documentation-only deprecation (DEP0205). Use

lib/internal/modules/esm/loader.js

Lines changed: 12 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,15 @@ function isCascadedLoaderInitialized() {
955958
* });
956959
* ```
957960
*/
961+
const emitRegisterDeprecation = getDeprecationWarningEmitter(
962+
'DEP0205',
963+
'`module.register()` is deprecated. Use `module.registerHooks()` instead.',
964+
undefined,
965+
false,
966+
);
967+
958968
function register(specifier, parentURL = undefined, options) {
969+
emitRegisterDeprecation();
959970
if (parentURL != null && typeof parentURL === 'object' && !isURL(parentURL)) {
960971
options = parentURL;
961972
parentURL = options.parentURL;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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() deprecation (DEP0205)', { concurrency: !process.env.TEST_PARALLEL }, () => {
13+
it('emits DEP0205 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.match(stderr, /\[DEP0205\]/);
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('[DEP0205]').length - 1, 1);
34+
assert.strictEqual(code, 0);
35+
});
36+
37+
it('does not emit when --no-deprecation is set', async () => {
38+
const { code, stderr } = await spawnPromisified(execPath, [
39+
'--no-deprecation',
40+
'--input-type=module',
41+
'--eval',
42+
`import { register } from 'node:module'; register(${urlToRegisterEscaped});`,
43+
]);
44+
45+
assert.doesNotMatch(stderr, /DEP0205/);
46+
assert.strictEqual(code, 0);
47+
});
48+
49+
it('throws when --throw-deprecation is set', async () => {
50+
const { code, stderr } = await spawnPromisified(execPath, [
51+
'--throw-deprecation',
52+
'--input-type=module',
53+
'--eval',
54+
`import { register } from 'node:module'; register(${urlToRegisterEscaped});`,
55+
]);
56+
57+
assert.match(stderr, /DEP0205/);
58+
assert.notStrictEqual(code, 0);
59+
});
60+
});

0 commit comments

Comments
 (0)