Skip to content

Commit df3b47f

Browse files
committed
modules: runtime deprecate module.parent
1 parent 117e293 commit df3b47f

8 files changed

+64
-14
lines changed

doc/api/deprecations.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2612,14 +2612,17 @@ no longer required due to simplification of the implementation.
26122612
### DEP0144: `module.parent`
26132613
<!-- YAML
26142614
changes:
2615+
- version: REPLACEME
2616+
pr-url: https://github.com/nodejs/node/pull/33534
2617+
description: Runtime deprecation.
26152618
- version:
26162619
- v14.6.0
26172620
- v12.19.0
26182621
pr-url: https://github.com/nodejs/node/pull/32217
26192622
description: Documentation-only deprecation.
26202623
-->
26212624

2622-
Type: Documentation-only (supports [`--pending-deprecation`][])
2625+
Type: Runtime (supports [`--pending-deprecation`][])
26232626

26242627
A CommonJS module can access the first module that required it using
26252628
`module.parent`. This feature is deprecated because it does not work
@@ -2643,6 +2646,9 @@ const moduleParents = Object.values(require.cache)
26432646
.filter((m) => m.children.includes(module));
26442647
```
26452648

2649+
Without `--pending-deprecation`, runtime warnings occur only when this property
2650+
is accessed from a module that have a nullish value for their `module.parent`.
2651+
26462652
### DEP0145: `socket.bufferSize`
26472653
<!-- YAML
26482654
changes:

doc/api/modules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -917,10 +917,10 @@ deprecated:
917917
> Stability: 0 - Deprecated: Please use [`require.main`][] and
918918
> [`module.children`][] instead.
919919
920-
* {module | null | undefined}
920+
* {module | null | UnknownModule}
921921

922922
The module that first required this one, or `null` if the current module is the
923-
entry point of the current process, or `undefined` if the module was loaded by
923+
entry point of the current process, or `UnknownModule` if the module was loaded by
924924
something that is not a CommonJS module (E.G.: REPL or `import`).
925925

926926
### `module.path`

lib/internal/modules/cjs/loader.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,26 +241,37 @@ ObjectDefineProperty(Module.prototype, 'isPreloading', isPreloadingDesc);
241241
ObjectDefineProperty(NativeModule.prototype, 'isPreloading', isPreloadingDesc);
242242

243243
function getModuleParent() {
244-
return moduleParentCache.get(this);
244+
const parent = moduleParentCache.get(this);
245+
if (!process.noDeprecation && !getModuleParent.warned &&
246+
(parent == null || pendingDeprecation)) {
247+
process.emitWarning(
248+
'module.parent is deprecated due to accuracy issues. Please use ' +
249+
'require.main to find program entry point instead.',
250+
'DeprecationWarning',
251+
'DEP0144'
252+
);
253+
getModuleParent.warned = true;
254+
}
255+
if (parent === undefined) {
256+
const parent = new (class UnknownModule {})();
257+
moduleParentCache.set(this, parent);
258+
return parent;
259+
}
260+
return parent;
245261
}
246262

247263
function setModuleParent(value) {
248264
moduleParentCache.set(this, value);
249265
}
250266

251267
ObjectDefineProperty(Module.prototype, 'parent', {
252-
get: pendingDeprecation ? deprecate(
253-
getModuleParent,
254-
'module.parent is deprecated due to accuracy issues. Please use ' +
255-
'require.main to find program entry point instead.',
256-
'DEP0144'
257-
) : getModuleParent,
258-
set: pendingDeprecation ? deprecate(
268+
get: getModuleParent,
269+
set: deprecate(
259270
setModuleParent,
260271
'module.parent is deprecated due to accuracy issues. Please use ' +
261272
'require.main to find program entry point instead.',
262273
'DEP0144'
263-
) : setModuleParent,
274+
),
264275
});
265276

266277
let debug = require('internal/util/debuglog').debuglog('module', (fn) => {

test/fixtures/cjs-module-parent.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
const assert = require('assert');
3+
4+
// Accessing module.parent should not be falsy.
5+
assert.ok(module.parent)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
require('../common');
4+
const fixtures = require('../common/fixtures');
5+
6+
// Accessing module.parent from a child module should not raise a warning.
7+
require(fixtures.path('cjs-module-parent.js'));
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { mustCall } from '../common/index.mjs';
2+
import fixtures from '../common/fixtures.js';
3+
4+
// Accessing module.parent from a child module should raise a deprecation
5+
// warning when imported from ESM.
6+
import(fixtures.path('cjs-module-parent.js'))
7+
.then(mustCall());

test/parallel/test-module-parent-deprecation.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
// Flags: --pending-deprecation
2-
31
'use strict';
2+
43
const common = require('../common');
54
const assert = require('assert');
65

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Flags: --pending-deprecation
2+
3+
'use strict';
4+
5+
const common = require('../common');
6+
const fixtures = require('../common/fixtures');
7+
8+
common.expectWarning(
9+
'DeprecationWarning',
10+
'module.parent is deprecated due to accuracy issues. Please use ' +
11+
'require.main to find program entry point instead.',
12+
'DEP0144'
13+
);
14+
15+
require(fixtures.path('cjs-module-parent.js'));

0 commit comments

Comments
 (0)