nodejs / nodejs/node

Wrong error annotation when commonjs `require`s an ES module

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

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

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

説明

Version
  • ^22.4.0
  • main with the --no-experimental-require-module flag.
Platform
macOS arm64
Subsystem

esm,module

What steps will reproduce the bug?
mkdir undefined && cd undefined
echo '{"type":"module"}' > package.json
echo "import nothing from 'somewhere'" > app.js
echo "require('./app.js')" > test.cjs
node test.cjs
How often does it reproduce? Is there a required condition?

It starts to happen with v22.4.0. v20.x and <=22.3.0 are not affected.

What is the expected behavior? Why is that the expected behavior?
$ node test.cjs
/Users/mzasso/git/test/undefined/test.cjs:1
require('./app.js')
^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/mzasso/git/test/undefined/app.js from /Users/mzasso/git/test/undefined/test.cjs not supported.
Instead change the require of app.js in /Users/mzasso/git/test/undefined/test.cjs to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/mzasso/git/test/undefined/test.cjs:1:1) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v22.3.0
What do you see instead?
$ node test.cjs
/Users/mzasso/git/test/undefined/test.cjs:315
undefined
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/mzasso/git/test/undefined/app.js from /Users/mzasso/git/test/undefined/test.cjs not supported.
Instead change the require of app.js in /Users/mzasso/git/test/undefined/test.cjs to a dynamic import() which is available in all CommonJS modules.
    at TracingChannel.traceSync (node:diagnostics_channel:315:14)
    at Object.<anonymous> (/Users/mzasso/git/test/undefined/test.cjs:1:1) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v22.4.0
Additional information

As you can see, this seems to be caused by the presence of TracingChannel.traceSync in the stack trace.

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

まず、Node.js v22.4.0 と --no-experimental-require-module フラグを使用して issue の再現コマンドを実行し、続いて ESM/CommonJS の require エラーと TracingChannel.traceSync のスタックアノテーションを追跡します。完了条件は、エラーアノテーションが期待される出力と同様に require('./app.js') の行を指し、報告されたバージョン変更をカバーするリグレッションテストがあることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
backend
issue の種類
バグ
難易度
4/5
見積もり時間
3〜5日
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
38/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。