Back/NodeJS

[NodeJS] SyntaxError: await is only valid in async functions and the top level bodies of modules

이영애님 2022. 12. 15. 22:18

#

const pageHTML = await axios.get("https://scrapeme.live/shop");
                 ^^^^^

SyntaxError: await is only valid in async functions and the top level bodies of modules
    at Object.compileFunction (node:vm:360:18)
    at wrapSafe (node:internal/modules/cjs/loader:1088:15)
    at Module._compile (node:internal/modules/cjs/loader:1123:27)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47

Node.js v18.11.0

 

##

await 는 async function 안에서 사용 가능하다. async function 으로 감싼다.

 

###

async function main(){
    const pageHTML = await axios.get("https://scrapeme.live/shop");
    //...
}

main();