Skip to content

Commit 0dfdec9

Browse files
module: runtime-deprecate module.register()
PR-URL: #62401 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Zeyu "Alex" Yang <himself65@outlook.com> Reviewed-By: Gürgün Dayıoğlu <hey@gurgun.day> Reviewed-By: Filip Skokan <panva.ip@gmail.com> Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Jacob Smith <jacob@frende.me>
1 parent 12c2736 commit 0dfdec9

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

doc/api/deprecations.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4527,16 +4527,27 @@ deprecated and will throw an error in a future version.
45274527
45284528
<!-- YAML
45294529
changes:
4530+
- version: REPLACEME
4531+
pr-url: https://github.com/nodejs/node/pull/62401
4532+
description: Runtime deprecation.
45304533
- version: REPLACEME
45314534
pr-url: https://github.com/nodejs/node/pull/62395
45324535
description: Documentation-only deprecation.
45334536
-->
45344537
4535-
Type: Documentation-only
4538+
Type: Runtime
45364539
45374540
[`module.register()`][] is deprecated. Use [`module.registerHooks()`][]
45384541
instead.
45394542
4543+
The `module.register()` API provides off-thread async hooks for customizing ES modules;
4544+
the `module.registerHooks()` API provides similar hooks that are synchronous, in-thread, and
4545+
work for all types of modules.
4546+
Supporting async hooks has proven to be complex, involving worker threads orchestration, and there are issues
4547+
that have proven unresolveable. See [caveats of asynchronous customization hooks][]. Please migrate to
4548+
`module.registerHooks()` as soon as possible as `module.register()` will be
4549+
removed in a future version of Node.js.
4550+
45404551
[DEP0142]: #dep0142-repl_builtinlibs
45414552
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
45424553
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3
@@ -4696,6 +4707,7 @@ instead.
46964707
[`zlib.bytesWritten`]: zlib.md#zlibbyteswritten
46974708
[alloc]: buffer.md#static-method-bufferallocsize-fill-encoding
46984709
[alloc_unsafe_size]: buffer.md#static-method-bufferallocunsafesize
4710+
[caveats of asynchronous customization hooks]: module.md#caveats-of-asynchronous-customization-hooks
46994711
[from_arraybuffer]: buffer.md#static-method-bufferfromarraybuffer-byteoffset-length
47004712
[from_string_encoding]: buffer.md#static-method-bufferfromstring-encoding
47014713
[legacy URL API]: url.md#legacy-url-api

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/62401
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: 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 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();
970+
959971
if (parentURL != null && typeof parentURL === 'object' && !isURL(parentURL)) {
960972
options = parentURL;
961973
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+
});

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-deprecation',
1011
'--no-experimental-require-module',
1112
'--import',
1213
fixtures.fileURL('es-module-loaders/builtin-named-exports.mjs'),

0 commit comments

Comments
 (0)