[英]Uncaught ReferenceError: require is not defined at app.js:3
在將前端HTML連接到私有區塊鏈以與智能合約進行交互時出現問題。
步驟完成如下:
npm install -g web3
//示例app.js文件代碼
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;
您應該將其安裝為依賴項,嘗試npm install --save web3
。 另外,您還需要webpack來翻譯您的javascript,因為瀏覽器無法理解require
語法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.