Web3js – How to Use Specific Version of Web3

truffleweb3js

I have listed the versions of web3 versions installed in my system

 npm ls web3
        inbox@1.0.0 /home/mdvenkatesh/Desktop/etherium solidity programes/inbox
        +-- truffle-hdwallet-provider@0.0.6
        | `-- web3@0.18.4 
        `-- web3@1.0.0-beta.26 

In my code i am using async await. i want to compile my code and run

But i am getting error

accounts = await web3.eth.getAccounts();
           ^^^^^

SyntaxError: await is only valid in async function

1) How to use version 1 without uninstalling the previous version of web3

2)In My previous projects i am using promise's if i uninstall the old version will it effect my previous code while compiling

3) I tried to remove using npm but showing me error

npm remove web3@0.18.4
npm WARN truffle-hdwallet-provider@0.0.6 requires a peer of truffle@4.x but none is installed. You must install peer dependencies yourself.
npm WARN inbox@1.0.0 No description
npm WARN inbox@1.0.0 No repository field.

Best Answer

The problem is not related to web3js at all. You need to mark function(lets name the function ‘check’) as ‘async’ before you call functions with await inside of function ‘check’. Take a look the documentation https://javascript.info/async-await

Related Topic