Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Looks like module.parent is deprecated #157

Closed
papb opened this issue Jul 23, 2020 · 2 comments · Fixed by #181
Closed

Looks like module.parent is deprecated #157

papb opened this issue Jul 23, 2020 · 2 comments · Fixed by #181

Comments

@papb
Copy link

papb commented Jul 23, 2020

I just saw it in the Release Notes for Node v14.6.0. PR: nodejs/node#32217

It is used here:

meow/index.js

Lines 13 to 15 in fa2a374

// Prevent caching of this module so module.parent is always accurate
delete require.cache[__filename];
const parentDir = path.dirname(module.parent.filename);

@aduh95
Copy link

aduh95 commented Jul 23, 2020

Note that module.parent is undefined when the module is loaded by something other than required from a CJS module (E.G.: await import('meow') or import meow from 'meow'), in which case the line 15 would throw (as reported in #130).

The deprecation note gives an alternate syntax to be able to get to the parent modules without altering the CJS cache:

const moduleParents = Object.values(require.cache)
  .filter((m) => m.children.includes(module));

Not sure if that would work here though; if there are several modules that loads meow, I don't think there is a reliable way to assess which made the call…

Ultimately, it seems to me there are three ways of looking at it:

  • keep using module.parent even though it's deprecated. It's been around for so long, chances are it will never go away. The workaround for ESM users is a bit ugly, but it works and it is well documented.
  • make a breaking change to the API, and request users to provide the dir path themselves (either by __dirname or import.meta.url) when calling meow().
  • Use the alternate syntax involving require.cache and module.children, and give up if meow has been loaded by more than one module. If several modules tries to load it, they need to provide the dir path in the options.

@sindresorhus
Copy link
Owner

make a breaking change to the API, and request users to provide the dir path themselves (either by __dirname or import.meta.url) when calling meow().

This is the best solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants