nodejs / nodejs/node

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

オープン
#58,487 コメント 15 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

never-stale
主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

リンクされた再現コードの test.js を Node 10 と Node 12+ で実行し、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 を短くまとめたダイジェスト。