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

377
Visualizações
Node.js showing unclear error when using 'await' without 'async'

I had an error with this code

exports.getResults = (req, res) => {
    status = await miDealApi.getResults()
    res.json({
        status: status
    })
}

The issue was that i used 'await' without declaring the method as 'async', but the error Node.js was giving me was very unclear, and didn't help me figuring it out at all.

internal/modules/cjs/loader.js:999
    throw err;
    ^

[wrapSafe (internal/modules/cjs/loader.js:979:16),Module._compile
(internal/modules/cjs/loader.js:1027:27),Object.Module._extensions..js
(internal/modules/cjs/loader.js:1092:10),Module.load 
(internal/modules/cjs/loader.js:928:32),Function.Module._load
(internal/modules/cjs/loader.js:769:14),Module.require
(internal/modules/cjs/loader.js:952:19),require
     (internal/modules/cjs/helpers.js:88:18),Object.<anonymous> (C:\Users\user\Documents\DT\projects\suresort-manager\controllers\waveFactory.js:6:30),Module._compile 
(internal/modules/cjs/loader.js:1063:30),Object.Module._extensions..js
(internal/modules/cjs/loader.js:1092:10)]
PS C:\Users\user\Documents\DT\projects\suresort-manager>

Is there anything i can do to make errors like this clearer in the future?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

One possibility is that your node.js version is a bit older, and thus, doesn't give you the proper error.

Running with Node 12 I get:

/project/foo.js:2
  status = await miDealApi.getResults()
           ^^^^^

SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)
    at internal/main/run_main_module.js:17:47

So you might want to check your version?

node -v
12.21.0

You should always aim for working with at least the stable version.

over 4 years ago · Santiago Trujillo Relatório

0

Keep in mind: await has to use with async.

An async function is a function declared with the async keyword, and the await keyword is permitted within them. The async and await keywords enable asynchronous, promise-based behavior to be written in a cleaner style, avoiding the need to explicitly configure promise chains.

Change your code like below:

exports.getResults = async (req, res) => {
    status = await miDealApi.getResults()
    res.json({
        status: status
    })
}

Happy coding :)

over 4 years ago · Santiago Trujillo 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