Skip to content

modules: runtime deprecate module.parent #33534

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2612,14 +2612,17 @@ no longer required due to simplification of the implementation.
### DEP0144: `module.parent`
<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/33534
description: Runtime deprecation.
- version:
- v14.6.0
- v12.19.0
pr-url: https://github.com/nodejs/node/pull/32217
description: Documentation-only deprecation.
-->

Type: Documentation-only (supports [`--pending-deprecation`][])
Type: Runtime (supports [`--pending-deprecation`][])

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

Without `--pending-deprecation`, runtime warnings occur only when this property
is accessed from a module that have a nullish value for their `module.parent`.

### DEP0145: `socket.bufferSize`
<!-- YAML
changes:
Expand Down
24 changes: 15 additions & 9 deletions lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,26 +241,32 @@ ObjectDefineProperty(Module.prototype, 'isPreloading', isPreloadingDesc);
ObjectDefineProperty(NativeModule.prototype, 'isPreloading', isPreloadingDesc);

function getModuleParent() {
return moduleParentCache.get(this);
const parent = moduleParentCache.get(this);
if (!process.noDeprecation && !getModuleParent.warned &&
(parent == null || pendingDeprecation)) {
process.emitWarning(
'module.parent is deprecated due to accuracy issues. Please use ' +
'require.main to find program entry point instead.',
'DeprecationWarning',
'DEP0144'
);
getModuleParent.warned = true;
}
return parent;
}

function setModuleParent(value) {
moduleParentCache.set(this, value);
}

ObjectDefineProperty(Module.prototype, 'parent', {
get: pendingDeprecation ? deprecate(
getModuleParent,
'module.parent is deprecated due to accuracy issues. Please use ' +
'require.main to find program entry point instead.',
'DEP0144'
) : getModuleParent,
set: pendingDeprecation ? deprecate(
get: getModuleParent,
set: deprecate(
setModuleParent,
'module.parent is deprecated due to accuracy issues. Please use ' +
'require.main to find program entry point instead.',
'DEP0144'
) : setModuleParent,
),
});

let debug = require('internal/util/debuglog').debuglog('module', (fn) => {
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/cjs-module-parent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
'use strict';

if(!module.parent) {
setImmediate(() => {});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict';

require('../common');
const fixtures = require('../common/fixtures');

// Accessing module.parent from a child module should not raise a warning.
require(fixtures.path('cjs-module-parent.js'));
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { mustCall } from '../common/index.mjs';
import fixtures from '../common/fixtures.js';

// Accessing module.parent from a child module should raise a deprecation
// warning when imported from ESM.
import(fixtures.path('cjs-module-parent.js'))
.then(mustCall());
3 changes: 1 addition & 2 deletions test/parallel/test-module-parent-deprecation.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Flags: --pending-deprecation

'use strict';

const common = require('../common');
const assert = require('assert');

Expand Down
15 changes: 15 additions & 0 deletions test/parallel/test-module-parent-pending-deprecation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Flags: --pending-deprecation

'use strict';

const common = require('../common');
const fixtures = require('../common/fixtures');

common.expectWarning(
'DeprecationWarning',
'module.parent is deprecated due to accuracy issues. Please use ' +
'require.main to find program entry point instead.',
'DEP0144'
);

require(fixtures.path('cjs-module-parent.js'));
2 changes: 0 additions & 2 deletions test/parallel/test-module-parent-setter-deprecation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Flags: --pending-deprecation

'use strict';
const common = require('../common');

Expand Down