diff --git a/doc/api/modules.md b/doc/api/modules.md index 9138826fd2f9ee..05135fb0b7dec8 100644 --- a/doc/api/modules.md +++ b/doc/api/modules.md @@ -913,7 +913,7 @@ deprecated: 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`). Read only. +something that is not a CommonJS module (E.G.: REPL or `import`). ### `module.path` <!-- YAML diff --git a/lib/internal/modules/cjs/loader.js b/lib/internal/modules/cjs/loader.js index 9b5752e1d265f6..051cc23277865f 100644 --- a/lib/internal/modules/cjs/loader.js +++ b/lib/internal/modules/cjs/loader.js @@ -223,13 +223,24 @@ ObjectDefineProperty(Module, 'wrapper', { function getModuleParent() { return moduleParentCache.get(this); } + +function setModuleParent(value) { + moduleParentCache.set(this, value); +} + ObjectDefineProperty(Module.prototype, 'parent', { get: pendingDeprecation ? deprecate( getModuleParent, 'module.parent is deprecated due to accuracy issues. Please use ' + 'require.main to find program entry point instead.', 'DEP0144' - ) : getModuleParent + ) : getModuleParent, + set: pendingDeprecation ? deprecate( + setModuleParent, + 'module.parent is deprecated due to accuracy issues. Please use ' + + 'require.main to find program entry point instead.', + 'DEP0144' + ) : setModuleParent, }); let debug = require('internal/util/debuglog').debuglog('module', (fn) => { diff --git a/test/parallel/test-module-parent-setter-deprecation.js b/test/parallel/test-module-parent-setter-deprecation.js new file mode 100644 index 00000000000000..f3efd89653cc8b --- /dev/null +++ b/test/parallel/test-module-parent-setter-deprecation.js @@ -0,0 +1,13 @@ +// Flags: --pending-deprecation + +'use strict'; +const common = require('../common'); + +common.expectWarning( + 'DeprecationWarning', + 'module.parent is deprecated due to accuracy issues. Please use ' + + 'require.main to find program entry point instead.', + 'DEP0144' +); + +module.parent = undefined;