Skip to content

Commit 729799d

Browse files
aduh95BethGriggs
authored andcommitted
module: deprecate module.parent
This feature does not work when a module is imported using ECMAScript modules specification, therefore it is deprecated. Fixes: nodejs/modules#469 PR-URL: #32217 Backport-PR-URL: #34592 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Bradley Farias <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent e5049f2 commit 729799d

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

doc/api/deprecations.md

+35
Original file line numberDiff line numberDiff line change
@@ -2560,6 +2560,41 @@ written twice. This introduces a race condition between threads, and is a
25602560
potential security vulnerability. There is no safe, cross-platform alternative
25612561
API.
25622562

2563+
<a id="DEP0144"></a>
2564+
### DEP0144: `module.parent`
2565+
<!-- YAML
2566+
changes:
2567+
- version:
2568+
- REPLACEME
2569+
- v14.6.0
2570+
pr-url: https://github.com/nodejs/node/pull/32217
2571+
description: Documentation-only deprecation.
2572+
-->
2573+
2574+
Type: Documentation-only
2575+
2576+
A CommonJS module can access the first module that required it using
2577+
`module.parent`. This feature is deprecated because it does not work
2578+
consistently in the presence of ECMAScript modules and because it gives an
2579+
inaccurate representation of the CommonJS module graph.
2580+
2581+
Some modules use it to check if they are the entry point of the current process.
2582+
Instead, it is recommended to compare `require.main` and `module`:
2583+
2584+
```js
2585+
if (require.main === module) {
2586+
// Code section that will run only if current file is the entry point.
2587+
}
2588+
```
2589+
2590+
When looking for the CommonJS modules that have required the current one,
2591+
`require.cache` and `module.children` can be used:
2592+
2593+
```js
2594+
const moduleParents = Object.values(require.cache)
2595+
.filter((m) => m.children.includes(module));
2596+
```
2597+
25632598
[`--http-parser=legacy`]: cli.html#cli_http_parser_library
25642599
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
25652600
[`--throw-deprecation`]: cli.html#cli_throw_deprecation

doc/api/modules.md

+12-2
Original file line numberDiff line numberDiff line change
@@ -894,11 +894,19 @@ loading.
894894
### `module.parent`
895895
<!-- YAML
896896
added: v0.1.16
897+
deprecated:
898+
- REPLACEME
899+
- v14.6.0
897900
-->
898901

899-
* {module}
902+
> Stability: 0 - Deprecated: Please use [`require.main`][] and
903+
> [`module.children`][] instead.
904+
905+
* {module | null | undefined}
900906

901-
The module that first required this one.
907+
The module that first required this one, or `null` if the current module is the
908+
entry point of the current process, or `undefined` if the module was loaded by
909+
something that is not a CommonJS module (E.G.: REPL or `import`).
902910

903911
### `module.path`
904912
<!-- YAML
@@ -1122,13 +1130,15 @@ consists of the following keys:
11221130
[`createRequire()`]: #modules_module_createrequire_filename
11231131
[`module` object]: #modules_the_module_object
11241132
[`module.id`]: #modules_module_id
1133+
[`module.children`]: #modules_module_children
11251134
[`path.dirname()`]: path.html#path_path_dirname_path
11261135
[ECMAScript Modules]: esm.html
11271136
[an error]: errors.html#errors_err_require_esm
11281137
[exports shortcut]: #modules_exports_shortcut
11291138
[module resolution]: #modules_all_together
11301139
[module wrapper]: #modules_the_module_wrapper
11311140
[native addons]: addons.html
1141+
[`require.main`]: #modules_require_main
11321142
[source map include directives]: https://sourcemaps.info/spec.html#h.lmz475t4mvbx
11331143
[`--enable-source-maps`]: cli.html#cli_enable_source_maps
11341144
[`NODE_V8_COVERAGE=dir`]: cli.html#cli_node_v8_coverage_dir

0 commit comments

Comments
 (0)