What is the use of module.parent in node.js? How can I refer to the require()ing module?

The “parent” is the module that caused the script to be interpreted (and cached), if any:

// $ node foo.js
console.log(module.parent); // `null`
// require('./foo')
console.log(module.parent); // `{ ... }`

What you’re expecting is the “caller,” which Node doesn’t retain for you. For that, you’ll need the exported function you’re currently using to be a closure for the value.

Leave a Comment