jaredhanson / jaredhanson/node-parent-require
module.parent is deprecated and doesn't work with ESM loader
- Dominant language
- JavaScript
- Stars
- 34
- Forks
- 5
- PR merge metrics
- No merged PRs in 30d
Description
As a result of nodejs/node#32217, [`module.parent`](https://nodejs.org/api/modules.html#modules_module_parent) is deprecated ([DEP0144](https://nodejs.org/api/deprecations.html#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](https://nodejs.org/api/deprecations.html#DEP0144) docs is to use `require.cache` with `module.children` to find parents:
```js
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`](https://nodejs.org/api/esm.html#esm_no_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
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the documented `node --pending-deprecation` command and compare its behavior with the ESM-loader limitation described in the issue. Determine whether parent-require can identify a parent without `module.parent` or `require.cache`; done requires a defined compatible behavior and validation for the reported command.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100