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

module.parent is deprecated and doesn't work with ESM loader #4

Open
kevinoid opened this issue May 29, 2021 · 0 comments
Open

module.parent is deprecated and doesn't work with ESM loader #4

kevinoid opened this issue May 29, 2021 · 0 comments

Comments

@kevinoid
Copy link

As a result of nodejs/node#32217, module.parent is deprecated (DEP0144), at least in part because it is not set for a CJS module imported using the ESM loader. The deprecation causes parent-require to print a warning if node is run with --pending-deprecation (or throw if run with --throw-deprecation):

node --pending-deprecation -e "require('parent-require')('parent-require')"
(node:483687) [DEP0144] DeprecationWarning: module.parent is deprecated due to accuracy issues. Please use require.main to find program entry point instead.
(Use `node --trace-deprecation ...` to show where the warning was created)

The suggestion in the DEP0144 docs is to use require.cache with module.children to find parents:

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

Unfortunately, I don't think this would work with the ESM loader, which does not use require.cache. I'm not sure there is a way to support the ESM loader, but I thought it was worth opening this issue so that you are aware.

Thanks,
Kevin

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

No branches or pull requests

1 participant