0

Having issue while connecting front-end HTML to private blockchain in order to interact with the smart contract.

Step completed as below:

  • created a smart contract and deployed in pvt blockchain.
  • npm install -g web3
  • created HTML file and linked with below js file.

// Sample app.js file code

var Web3 = require('web3');
var web3 = new Web3();

if (typeof web3 !== 'undefined') {alert(" web3.currentProvider :: " + web3.currentProvider);
    web3 = new Web3(web3.currentProvider);
} else {
    // set the provider you want from Web3.providers
    web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
 }

 var abi= My-custom-ABI;


 var ins=web3.eth.contract(abi);
 var instance= ins.at("My-Contract-Address");
 var accounts;
 var account;
 var account2;
TylerH
  • 20,799
  • 66
  • 75
  • 101
  • 1
    Do you have any kind of build process or did you refer to the `app.js` file as-is from your HTML page? – user247702 Oct 08 '18 at 16:32
  • using with typescript, you should follow different syntax, import Web3 = require("web3"); // Note the special syntax! Copy this line when in doubt! – Naga Sai A Oct 08 '18 at 16:34
  • and also install @types/web3.js using npm install --dev @types/web3.js – Naga Sai A Oct 08 '18 at 16:34
  • check this link for more details - https://github.com/ethereum/web3.js/blob/1.0/README.md#usage-with-typescript – Naga Sai A Oct 08 '18 at 16:36
  • @NagaSaiA Even though the question is (probably incorrectly) tagged with TypeScript, the shown code is 'normal' ECMAScript/JavaScript.. (anyway, `var abi= My-custom-ABI;` is clearly wrong, but that's a different problem) – user2864740 Oct 08 '18 at 17:12

1 Answers1

0

You should install it as your dependency, try npm install --save web3. Also, you need webpack to transpile your javascript, because browser don't understand require syntax.

  • npm install --save web3 have installed it locally and globally.Then too issue persist. But i ran the js file using "nodemon" and checked the httml page. – Ajendra Nautiyal Oct 10 '18 at 08:50
  • @Stijin Have not used any build process to refer the js file. – Ajendra Nautiyal Oct 10 '18 at 08:56
  • By using nodemon, that means you're in nodejs environment. So you serve the page using node js. By default, nodejs support require syntax, it shouldn't have any problem with it. Can you provide more information about your package.json? like your npm script. Also, try to make another js file, and require it to see if the problem persist. Like so, var js = require('./another.js') – arman rozika Oct 10 '18 at 14:23