Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

244
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda