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

217
Visualizações
Top level await not working for Node 16.16.0?

I am converting my node app to use top level await for connecting to my Redis client but am getting the error "await is only valid in async functions and the top level bodies of modules".

I do not understand why I am getting this error since I am using Node version 16.16.0 and top level await should work for Node.js since v14.8??

Why is my code not working?

Versions used: NODE v16.16.0, Redis ^4.2.0

STACK TRACE

at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (/Users/app/git/app-node-api/src/index.js:4:1)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)

CODE

const Redis = require("redis")
const util = require(`util`)
const client = Redis.createClient({
  url: process.env.REDIS_CLIENT_PRIMARY,
})

client.on("error", async (err) => console.log("Redis Client Error", err))

await client.connect() // <- THROWS ERROR "await is only valid in async functions and the top level bodies of modules" 

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Top-level await is only available in ES modules. You can write a module by using the mjs extension instead of js or by setting { "type": "module" } in package.json.

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