Skip to content

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

Open
@kevinoid

Description

@kevinoid

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions