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

415
Visualizações
How to have top level await in typescript (switch from commonjs to esnext) without having to change all imports to have .js ending

I would like to have top level await in my typescript nodejs project.

My tsconfig used to look like this:

{
  "compilerOptions": {
    "target": "es2017",
    "module": "commonjs",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
#### other stuff which I think is not relevant removed ####

And I now switched it to

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "lib": [
      "dom",
      "es6",
      "es2017",
      "esnext.asynciterable"
    ],
    "skipLibCheck": true,
    "sourceMap": true,
    "outDir": "./dist",
    "moduleResolution": "node",
#### other stuff which I think is not relevant removed ####

I have also added "type": "module" to my package.json. Indeed I now have the ability to do top level awaits however

  1. I need to change every import to add the .js file extension
  2. For folders where I added an index.ts to export all the modules I could previously just import the folder name. Now I need to import foldername/index.js
  3. When I auto add an import with vscode it adds it without the .js

The way it is with commonjs is so elegant - can I have the same behaviour with esnext or keep it some other way while gaining top-level await?

about 4 years ago · Santiago Gelvez
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