Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

module.parent.filename is cached. #6149

Closed
nazomikan opened this issue Aug 29, 2013 · 7 comments
Closed

module.parent.filename is cached. #6149

nazomikan opened this issue Aug 29, 2013 · 7 comments

Comments

@nazomikan
Copy link

When a file is called twice, module.parent is cached.
Is this correct behavior?

/home/xxx/yyy/module_parent/
├── a.js
└── up
    ├── b.js
    └── up
        └── c.js

a.js

var b, c = require('./up/up/c');
c.dump();
b = require('./up/b')

b.js

var c = require('./up/c');
c.dump();

c.js

exports.dump = function () {
  console.log(module.parent.filename);
}

console

/home/xxx/yyy/module_parent/a.js
/home/xxx/yyy/module_parent/a.js
@bnoordhuis
Copy link
Member

When a file is called twice, module.parent is cached.
Is this correct behavior?

Yes, it is. The module gets evaluated only once so its parent is the module that first require()s it.

@nazomikan
Copy link
Author

I see... thank you!
Is there a way to access to the caller in any other way?

@isaacs
Copy link

isaacs commented Aug 29, 2013

Is there a way to access to the caller in any other way?

No. Please find another way around. Export a function, and call it with the relevant data if you care about this.

@isaacs
Copy link

isaacs commented Aug 29, 2013

eg:

// a.js
module.exports = function(caller) {
  doSomething(caller)
};

// b.js
var a = require('./a.js');
a(__filename);

@nazomikan
Copy link
Author

All right. thx!

Initially, I thought it's possible if change the cacheModule.parent in Module._load , but on second thought, i understood this was bad way.
I had forgotten that the bad impact would be out on the side that is already loaded.
To clone the cacheModule also is a cost of its own way, and I decide to give up:)

As of advice I have received from you, so i decide to give the file path.
Thank you carefully!!:)

@jakswa
Copy link

jakswa commented Jun 24, 2014

I ran into this unexpected behavior as well. I wish the property was called module.originalParent or something. Or maybe just have the docs explicitly state that it isn't "The module that required this one" but is actually, "the first module that required this one" or something.

@jorrit
Copy link

jorrit commented Jul 5, 2015

That last change @jakswa mentioned would be very welcome.

jorrit added a commit to jorrit/node that referenced this issue Jul 5, 2015
jasnell added a commit to jasnell/node that referenced this issue Aug 26, 2015
jasnell added a commit to nodejs/node that referenced this issue Aug 26, 2015
Per: jorrit/node@54a7f79

Fixes: nodejs/node-v0.x-archive#6149

Originally submitted by @jorrit

Reviewed By: Sakthipriyan Vairamani <[email protected]>
PR-URL: #2378
jasnell added a commit to nodejs/node that referenced this issue Aug 26, 2015
Per: jorrit/node@54a7f79

Fixes: nodejs/node-v0.x-archive#6149

Originally submitted by @jorrit

Reviewed By: Sakthipriyan Vairamani <[email protected]>
PR-URL: #2378
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants