nodejs / nodejs/node

Referencing external dependency via `package.json` works for ESM, but not CJS

Abierto
#49,257 7 comentarios 1 reacción 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Lenguaje dominante
JavaScript
Estrellas
122k
Forks
37.4k
Merge medio
4 d 3 h
PR fusionados (30 d)
272

Descripción

Version

v20.5.1

Platform

Linux vali 6.4.6-76060406-generic #202307241739169092810522.04~d567a38 SMP PREEMPT_DYNAMIC Tue A x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Clone https://gist.github.com/remcohaszing/25c719651d340d9170dd75428dbcef8e

Given the following files:

// package.json
{
  "imports": {
    "#path": {
      "default": "path"
    }
  }
}
// index.cjs
require('#path')
// index.mjs
await import('#path')

Now run both scripts.

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

It reproduces consistently.

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

Both scripts run and output nothing.

What do you see instead?

Running node index.mjs works fine, but node index.cjs throws:

node:internal/modules/cjs/loader:1018
        throw e;
        ^

TypeError [ERR_INVALID_URL_SCHEME]: The URL must be of scheme file
    at new NodeError (node:internal/errors:405:5)
    at fileURLToPath (node:internal/url:1397:11)
    at finalizeEsmResolution (node:internal/modules/cjs/loader:1059:20)
    at Module._resolveFilename (node:internal/modules/cjs/loader:1011:16)
    at Module._load (node:internal/modules/cjs/loader:901:27)
    at Module.require (node:internal/modules/cjs/loader:1115:19)
    at require (node:internal/modules/helpers:130:18)
    at Object.<anonymous> (/home/remco/Downloads/foo/index.cjs:1:1)
    at Module._compile (node:internal/modules/cjs/loader:1233:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1287:10) {
  code: 'ERR_INVALID_URL_SCHEME'
}

Node.js v20.5.1

Referencing a relative path works fine though.

Additional information

There was some discussion in this Twitter thread.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Reproduce el problema usando el package.json, index.cjs e index.mjs del gist y, después, sigue la resolución de CommonJS a través de node:internal/modules/cjs/loader, especialmente finalizeEsmResolution. Se considera terminado cuando el mapeo imports de package.json al path integrado funciona desde ambos scripts sin ERR_INVALID_URL_SCHEME.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
javascript, node.js
Área
backend
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Activo
Claridad
Bastante claro
Aptitud para principiantes
52/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.