Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

239
Vistas
Built-in nodejs path aliasing and implied index.js module resolution

I want to set up import path aliasing in a typescript/node/express project WITHOUT using other packages like tsconfig-paths and link-module-alias. I would like to do this with built-in nodejs functionality (the Typscript aliasing is already done).

Basically:

// Change
import { myFn } from '../../../utils';

// To
import { myFn } from '#this/utils';

The glimmer of hope I have is with node's subpath imports. Theoretically, I can just add this to my package.json:

"imports": {
    "#this/*": "./dist/*"
}

Here's the problem

This works for explicitly importing the *.js files, but it does fails for implied index.js (i.e. Folders as modules)

// Works
import { myFn } from '#this/utils/index.js';

// Does not work
import { myFn } from '#this/utils';

However, I would expect node to resolve the import like this:

  • if utils is a directory, the import should resolve to */utils/index.js
  • if utils is a file, the import should resolve to */utils.js

My first thought would be to update the package.json imports to include all possibilities, but this is not supported:

"imports": {
    "#this/*": [
        "./dist/*",
        "./dist/*.js",
        "./dist/*/index.js"
    ]
}

The other option seems to be to define an exports entry for every directory, but that is not scalable.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

With --experimental-specifier-resolution=node flag the code below works for me.

index.js:

import { myFn } from '#this';

package.json:

...
  "imports": {
    "#this": "./utils"
  }
...
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda