diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 842ebf97ec5aff..58168640cec6cc 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -2707,6 +2707,39 @@ Type: Runtime `Transform._transformState` will be removed in future versions where it is no longer required due to simplification of the implementation. + +### DEP0144: `module.parent` + + +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)); +``` + ### DEP0XXX: `socket.bufferSize` -* {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`