nodejs / nodejs/node

Provide a reliable way to find the entrypoint path or URL

Abierto
#51,840 21 comentarios 17 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature request loaders
Lenguaje dominante
JavaScript
Estrellas
122k
Forks
37.3k
Merge medio
4 d 2 h
PR fusionados (30 d)
283

Descripción

What is the problem this feature will solve?

This solves the same problem as require.main.filename did in a CommonJS-only world: finding the entrypoint script for the Node.js process. This is most commonly used to determine the whether the current file is the entrypoint, but is also useful for various other sorts of introspection about the running Node.js environment. My specific use-case is finding a sensible default base URL to use to replicate Node.js's package resolution algorithm.

What is the feature you are proposing to solve the problem?

I suggest something like process.cliEntrypoint or process.cliMainUrl. I think it's important that any solution be usable regardless of whether the current module is CJS or ESM.

What alternatives have you considered?

require.main.filename provides this information but it doesn't work outside of CJS files, nor does it work if the entrypoint itself is ESM.

process.argv[1] sometimes provides this information, but it has a number of problems:

  • If the Node.js CLI is run without a file extension, like node myapp instead of node myapp.js, process.argv[1] won't have an extension either.

  • If the entrypoint is a symlink, process.argv[1] will point to the symlink instead of the physical JS file. This differs from __filename, which means it also differs from the logic used by require(). Note that npm exec always creates a symlink when running executables.

  • process.argv is mutable, so it's possible for unrelated code to modify it in such a way that it's no longer usable to find the entrypoint.

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

No se ha indicado ningún archivo de implementación ni ninguna prueba. Empieza revisando cómo determina actualmente Node.js el punto de entrada de la CLI para CommonJS y ESM, incluidas las alternativas require.main.filename y process.argv[1] descritas aquí. Se considera terminado cuando una API estable funciona con ambos sistemas de módulos y admite rutas sin extensión, enlaces simbólicos y cambios posteriores en process.argv.

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
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Tranquilo
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.