module: deprecate module.parent

This feature does not work when a module is imported using ECMAScript
modules specification, therefore it is deprecated.

Fixes: https://github.com/nodejs/modules/issues/469

PR-URL: https://github.com/nodejs/node/pull/32217
Backport-PR-URL: https://github.com/nodejs/node/pull/34592
Reviewed-By: Gus Caplan <me@gus.host>
Reviewed-By: Bradley Farias <bradley.meck@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Antoine du HAMEL authored and Beth Griggs committed 2020-09-15 21:39:04 +01:00
1 parent 60b697de30
commit c9bd1a7d8a
2 files changed
+47 -2

No files matched your search

+35
View File
@@ -2560,6 +2560,41 @@ written twice. This introduces a race condition between threads, and is a
potential security vulnerability. There is no safe, cross-platform alternative
API.
<a id="DEP0144"></a>
### DEP0144: `module.parent`
<!-- YAML
changes:
- version:
- REPLACEME
- v14.6.0
pr-url: https://github.com/nodejs/node/pull/32217
description: Documentation-only deprecation.
-->
Type: Documentation-only
A CommonJS module can access the first module that required it using
`module.parent`. This feature is deprecated because it does not work
consistently in the presence of ECMAScript modules and because it gives an
inaccurate representation of the CommonJS module graph.
Some modules use it to check if they are the entry point of the current process.
Instead, it is recommended to compare `require.main` and `module`:
```js
if (require.main === module) {
// Code section that will run only if current file is the entry point.
}
```
When looking for the CommonJS modules that have required the current one,
`require.cache` and `module.children` can be used:
```js
const moduleParents = Object.values(require.cache)
.filter((m) => m.children.includes(module));
```
[`--http-parser=legacy`]: cli.html#cli_http_parser_library
[`--pending-deprecation`]: cli.html#cli_pending_deprecation
[`--throw-deprecation`]: cli.html#cli_throw_deprecation
+12 -2
View File
@@ -894,11 +894,19 @@ loading.
### `module.parent`
<!-- YAML
added: v0.1.16
deprecated:
- REPLACEME
- v14.6.0
-->
* {module}
> Stability: 0 - Deprecated: Please use [`require.main`][] and
> [`module.children`][] instead.
The module that first required this one.
* {module | null | undefined}
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`).
### `module.path`
<!-- YAML
@@ -1122,6 +1130,7 @@ consists of the following keys:
[`createRequire()`]: #modules_module_createrequire_filename
[`module` object]: #modules_the_module_object
[`module.id`]: #modules_module_id
[`module.children`]: #modules_module_children
[`path.dirname()`]: path.html#path_path_dirname_path
[ECMAScript Modules]: esm.html
[an error]: errors.html#errors_err_require_esm
@@ -1129,6 +1138,7 @@ consists of the following keys:
[module resolution]: #modules_all_together
[module wrapper]: #modules_the_module_wrapper
[native addons]: addons.html
[`require.main`]: #modules_require_main
[source map include directives]: https://sourcemaps.info/spec.html#h.lmz475t4mvbx
[`--enable-source-maps`]: cli.html#cli_enable_source_maps
[`NODE_V8_COVERAGE=dir`]: cli.html#cli_node_v8_coverage_dir