nodejs / nodejs/node

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

Aperta
#55,350 7 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

confirmed-bug
Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo i comandi per riprodurre il problema nell’issue con Node.js v22.4.0 e il flag --no-experimental-require-module, quindi traccia l’errore require di ESM/CommonJS e l’annotazione dello stack di TracingChannel.traceSync. Il lavoro è completato quando l’annotazione dell’errore punta alla riga require('./app.js') come nell’output previsto, con un test di regressione che copra la modifica di versione segnalata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
backend
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
38/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.