nodejs / nodejs/node

`require.resolve` regression starts from Node v12 with `paths`

未关闭
#58,487 15 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

never-stale
主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version

v20.19.2

Platform
Darwin MacBookPro 24.5.0 Darwin Kernel Version 24.5.0: Tue Apr 22 19:54:49 PDT 2025; root:xnu-11417.121.6~2/RELEASE_ARM64_T6000 arm64
Subsystem

No response

What steps will reproduce the bug?

https://github.com/JounQin/test/tree/repro/node_require_resolve_paths

Simply run node test.js

On node 10:

`resolve` npm package: <CWD>/fixtures/bar.js
`require.resolve` built-in function: <CWD>/fixtures/bar.js
How often does it reproduce? Is there a required condition?

Persist

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

There is a related old issue #29139.

What do you see instead?

On node 12+, of course including Node LTS (20+)

`resolve` npm package: /Users/JounQin/Workspaces/GitHub/test/fixtures/bar.js
node:internal/modules/cjs/loader:1404
  throw err;
  ^

Error: Cannot find module 'bar'
Require stack:
- <CWD>/test.js
    at Function._resolveFilename (node:internal/modules/cjs/loader:1401:15)
    at Function.resolve (node:internal/modules/helpers:145:19)
    at Object.<anonymous> (/Users/JounQin/Workspaces/GitHub/test/test.js:16:11)
    at Module._compile (node:internal/modules/cjs/loader:1730:14)
    at Object..js (node:internal/modules/cjs/loader:1895:10)
    at Module.load (node:internal/modules/cjs/loader:1465:32)
    at Function._load (node:internal/modules/cjs/loader:1282:12)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:235:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '<CWD>/test.js' ]
}
Additional information

I believe there is no reports because most projects use http://npmjs.com/resolve instead, and it behaviors same as Node 10-, and I'm trying to remove resolve as dependency, so I just met this issue at https://github.com/un-ts/eslint-plugin-import-x/pull/272#issuecomment-2915145320

cc @cjihrig

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

在 Node 10 和 Node 12+ 下运行链接的复现代码中的 test.js,并比较提供 paths 时的 require.resolve 结果。阅读堆栈跟踪中显示的 CommonJS 加载器路径以及相关 issue #29139;当复现代码无需外部 resolve 包即可一致地解析 fixtures/bar.js 时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
48/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。