11

Trying to use web3-utils.

I installed the package via NPM , however, I am getting the error that sha3 is undefined..

TypeError: Cannot read property 'sha3' of undefined
    at C:\block2\deliver.js:12:28
    at Layer.handle [as handle_request] (C:\block2\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\block2\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\block2\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\block2\node_modules\express\lib\router\layer.js:95:5)
    at C:\block2\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\block2\node_modules\express\lib\router\index.js:335:12)
    at next (C:\block2\node_modules\express\lib\router\index.js:275:10)
    at expressInit (C:\block2\node_modules\express\lib\middleware\init.js:40:5)
    at Layer.handle [as handle_request] (C:\block2\node_modules\express\lib\router\layer.js:95:5)

CODE:

var express = require('express');
var app = express();
Web3 = require("web3");
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

var util = require('web3-utils');



app.get('/endpoint', function(request, response) {
    var id = request.query.id;
    response.end(web3.utils.sha3('test'));
});

app.listen(3000, '127.0.0.1', function() {
    console.log('Listening to port:  ' + 3000);
});
  • 2
    What's your web3.version? web3.utils.sha3 is available in the latest 1.0.0-betas, but not in the "release" branch, 0.20. Which version is installed by npm changed recently. Best to check. – benjaminion Aug 12 '17 at 12:34
  • How can I check web3version? @benjaminion – alper Dec 29 '17 at 21:06
  • @Alper type web3.version in the node console (after loading web3 as above). – benjaminion Dec 29 '17 at 21:35
12

Please check your web3.js library version.

In versions < 1.0 you can call web3.sha3('test').
In 1.0 it was moved to web3.utils.sha3

Another option is to use web3-utils library, in this case you need to call utils.sha3('test').

  • I cannot do web3.utils.sha3(lengthSlot, {encoding: 'hex'}) or it just returns a different result from web3.sha3(lengthSlot, {encoding: 'hex'}) – Ender Sep 16 at 8:15
2

Check your version by this call: web3.version.api
For versions before 1.x.x, use function from this api.
there the example of correct code.web3.toAscii

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.