Skip to content

module: make module.parent truthy when loaded from ESM #37702

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 1 commit 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
13 changes: 10 additions & 3 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -912,16 +912,23 @@ added: v0.1.16
deprecated:
- v14.6.0
- v12.19.0
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/00000
description: When the module is loaded by something that is not a CommonJS
module, the value of `module.parent` is truthy. On previous
versions, it is `undefined`.
-->

> Stability: 0 - Deprecated: Please use [`require.main`][] and
> [`module.children`][] instead.

* {module | null | undefined}
* {module | null | Object}

The module that first required this one, or `null` if the current module is the
entry point of the current process, or `undefined` if the module was loaded by
something that is not a CommonJS module (E.G.: REPL or `import`).
entry point of the current process, or an instance of an add-hoc `UnknownModule`
class if the module was loaded by something that is not a CommonJS module (E.G.:
REPL or `import`).

### `module.path`
<!-- YAML
Expand Down
8 changes: 7 additions & 1 deletion lib/internal/modules/cjs/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,13 @@ ObjectDefineProperty(Module.prototype, 'isPreloading', isPreloadingDesc);
ObjectDefineProperty(NativeModule.prototype, 'isPreloading', isPreloadingDesc);

function getModuleParent() {
return moduleParentCache.get(this);
const parent = moduleParentCache.get(this);
if (parent === undefined) {
const parent = new (class UnknownModule {})();
moduleParentCache.set(this, parent);
return parent;
}
return parent;
}

function setModuleParent(value) {
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';
const assert = require('assert');

// Accessing module.parent should not be falsy.
assert.ok(module.parent)
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());