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);
});
benjaminion
  • 9,247
  • 1
  • 23
  • 36
Dino Anastos
  • 847
  • 1
  • 13
  • 22

2 Answers2

14

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').

max taldykin
  • 2,966
  • 19
  • 27
  • 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 '19 at 08: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

Avram
  • 121
  • 3