Skip to content

Commit

Permalink
module: deprecate module.parent
Browse files Browse the repository at this point in the history
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
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]>
  • Loading branch information
aduh95 committed Aug 18, 2020
1 parent bb0cc00 commit 573ad3e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
35 changes: 35 additions & 0 deletions doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions doc/api/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* {module | null | undefined}

The module that first required this one.
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
Expand Down Expand Up @@ -1122,13 +1130,15 @@ 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
[exports shortcut]: #modules_exports_shortcut
[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
Expand Down

0 comments on commit 573ad3e

Please sign in to comment.