0

I am trying to use node await syntax but keep getting errors. I run

web3 = new Web3(new Web3.providers.HttpProvider('https://mainnet.infura.io'));
let isListening = await web3.eth.net.isListening();
    let numPeers = await web3.eth.net.getPeerCount();
    console.log( "node " + infuraUrl + " listening: " + isListening.toString() + " with " + numPeers + " peers");

and I keep getting this error:

let isListening = await web3.eth.net.isListening();
                        ^^^^

SyntaxError: Unexpected identifier
    at createScript (vm.js:80:10)
    at Object.runInThisContext (vm.js:139:10)
    at Module._compile (module.js:607:28)
    at Object.Module._extensions..js (module.js:654:10)
    at Module.load (module.js:556:32)
    at tryModuleLoad (module.js:499:12)
    at Function.Module._load (module.js:491:3)
    at Function.Module.runMain (module.js:684:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
1

first verify you are using web3 version 1.x which supports promise.

web3 = new Web3(new Web3.providers.HttpProvider(infuraUrl));
console.log("web 3 version " + web3.version);

if u don't have 1.x version. refer to this question

syntax is correct but when using await, it must be placed inside an async function as such:

async function logMetaData(){
    let isListening = await web3.eth.net.isListening();
    let numPeers = await web3.eth.net.getPeerCount();
    console.log( "node " + infuraUrl + " listening: " + isListening.toString() + " with " + numPeers + " peers");
};

and then just call this, and your done.

logMetaData();
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.