Solidity – Trying to Use Web3-Utils But Not Working

hashsolidityweb3js

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);
});

Best Answer

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