nodejs / nodejs/node

`require.resolve()` and `import.meta.resolve()` doesn't work with self-referenced packages

Open
#47,681 11 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

module never-stale
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Version

v20.0.0

Platform

Linux executive 5.19.0-40-generic #41-Ubuntu SMP PREEMPT_DYNAMIC Thu Mar 23 21:39:15 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

require, import

What steps will reproduce the bug?

Call to require.resolve() created with createRequire(), or to import.meta.resolve(), passing the package name to resolve to itself.

How often does it reproduce? Is there a required condition?

Always.

What is the expected behavior? Why is that the expected behavior?

It should return the path of the package itself.

What do you see instead?
node:internal/errors:490
    ErrorCaptureStackTrace(err);
    ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@mafalda-sfu/mediasoup-cluster-cli' imported from /home/piranna/github/Mafalda/Mediasoup-cluster-CLI/node_modules/@mafalda-sfu/bytenodeify/lib/getBundlesFilenames.js
    at new NodeError (node:internal/errors:399:5)
    at packageResolve (node:internal/modules/esm/resolve:794:9)
    at moduleResolve (node:internal/modules/esm/resolve:843:20)
    at defaultResolve (node:internal/modules/esm/resolve:1070:11)
    at DefaultModuleLoader.resolve (node:internal/modules/esm/loader:306:12)
    at Object.resolve (node:internal/modules/esm/initialize_import_meta:17:25)
    at getBundlesFilenames_priv (file:///home/piranna/github/Mafalda/Mediasoup-cluster-CLI/node_modules/@mafalda-sfu/bytenodeify/lib/getBundlesFilenames.js:31:32)
    at file:///home/piranna/github/Mafalda/Mediasoup-cluster-CLI/node_modules/@mafalda-sfu/bytenodeify/lib/getBundlesFilenames.js:86:17
    at Array.map (<anonymous>)
    at default (file:///home/piranna/github/Mafalda/Mediasoup-cluster-CLI/node_modules/@mafalda-sfu/bytenodeify/lib/getBundlesFilenames.js:81:38) {
  code: 'ERR_MODULE_NOT_FOUND'
}
Additional information

Not sure if it happens with CommonJS require() too, or just only with ESM related resolve functions.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the ESM resolution entry points shown in the stack, including internal/modules/esm/resolve and internal/modules/esm/initialize_import_meta, and compare them with the createRequire() path. Reproduce the self-reference case from the issue, then add or update focused resolution tests so require.resolve() and import.meta.resolve() return the package's own path.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.