0

I want to test web3.js functionality, then I connect to Ganache, when I run the program web3.eth.getAccounts works properly, but web3.eth.accounts.signTransaction went wrong. I have no idea to solve it.

const Web3 = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));

web3.eth.getAccounts(
    function (error, result){ 
    if(!error){
      console.log(result);
    } else{
      console.log(error);
    }
});

web3.eth.accounts.signTransaction({
        to: '0x93......',
        value: '1000000000000000000',
        gas: 2000000,
    },'19ec......', function (error, result){ 
        if(!error){
          console.log(result);
        } else{
          console.log(error);
        }
});

It returns

TypeError: web3.eth.accounts.signTransaction is not a function
    at Object.<anonymous> (C:\BC\coursetro-eth\test_web3.js:302:19)
[90m    at Module._compile (internal/modules/cjs/loader.js:956:30)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:812:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:724:14)[39m
[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)[39m
[90m    at internal/main/run_main_module.js:17:11[39m

When I try another web3.eth.accounts, like web3.eth.accounts.hashMessage, it also returns the same problem.

web3.eth.accounts.hashMessage("Hello World", function (error, result){ 
    if(!error){
      console.log(result);
    } else{
      console.log(error);
    }
});

TypeError: web3.eth.accounts.hashMessage is not a function
    at Object.<anonymous> (C:\BC\coursetro-eth\test_web3.js:315:19)
[90m    at Module._compile (internal/modules/cjs/loader.js:956:30)[39m
[90m    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)[39m
[90m    at Module.load (internal/modules/cjs/loader.js:812:32)[39m
[90m    at Function.Module._load (internal/modules/cjs/loader.js:724:14)[39m
[90m    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)[39m
[90m    at internal/main/run_main_module.js:17:11[39m

This is the web3 version that I installed

"dependencies": {
     "web3": "^0.20.7"
}

1 Answer 1

2

I think you get these errors, because you're using too old web3.js version. Recording to this documentation web3.eth.accounts.signTransaction is part of web3.js 1.0.0. So just upgrade from 0.20.x to 1.0.x.

2
  • After I upgrade the web3 version, the problem has solved. Thank you.
    – leojail
    Dec 10, 2020 at 10:13
  • @lamigo6666 Great, please accept the answer with the green check, so others having the same issue can use the same solution. Dec 10, 2020 at 10:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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