TypeError: Cannot read property 'HttpProvider' of undefined while deploying solidity contract using nodejs
TypeError: Cannot read property 'HttpProvider' of undefined while deploying solidity contract using nodejs
我有下面的 nodejs 代码来将智能合约从文件部署到本地私有以太坊网络。
const fs = require('fs');
const Web3 = require('web3');
const solc = require('solc');
const web3 = new Web3();
web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
const address = web3.eth.accounts[0];
const code = fs.readFileSync('../Calculator.sol').toString()
const compiledCode = solc.compile(code)
const abiDefinition = JSON.parse(compiledCode.contracts[':Calculator'].interface)
const byteCode = compiledCode.contracts[':Calculator'].bytecode
const CalcContract = web3.eth.contract(abiDefinition)
我低于 package.json
中的依赖项
"dependencies": {
"ethereumjs-testrpc": "^6.0.3",
"fs": "0.0.1-security",
"solc": "^0.5.9",
"web3": "^1.0.0-beta.55"
}
当我 运行 程序时,我在第 web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
行遇到以下错误
[raj@localhost first_truffle_project]$ sudo node js/example_deployer.js
/home/raj/Coding/Ethereum/first_truffle_project/js/example_deployer.js:6
web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
^
TypeError: Cannot read property 'HttpProvider' of undefined
at Object.<anonymous> (/home/raj/Coding/Ethereum/first_truffle_project/js/example_deployer.js:6:33)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
我的程序有什么问题?我该如何解决这个错误?
这样试试:
web3 = new Web3(new Web3.providers.HttpProvider('node-url'))
确保您在 package.json 中的 Web 3 版本是
"web3": "^1.0.0-beta.37"
我相信你想要这样的东西。 (注意大小写。)
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
我有下面的 nodejs 代码来将智能合约从文件部署到本地私有以太坊网络。
const fs = require('fs');
const Web3 = require('web3');
const solc = require('solc');
const web3 = new Web3();
web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
const address = web3.eth.accounts[0];
const code = fs.readFileSync('../Calculator.sol').toString()
const compiledCode = solc.compile(code)
const abiDefinition = JSON.parse(compiledCode.contracts[':Calculator'].interface)
const byteCode = compiledCode.contracts[':Calculator'].bytecode
const CalcContract = web3.eth.contract(abiDefinition)
我低于 package.json
中的依赖项 "dependencies": {
"ethereumjs-testrpc": "^6.0.3",
"fs": "0.0.1-security",
"solc": "^0.5.9",
"web3": "^1.0.0-beta.55"
}
当我 运行 程序时,我在第 web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
行遇到以下错误
[raj@localhost first_truffle_project]$ sudo node js/example_deployer.js
/home/raj/Coding/Ethereum/first_truffle_project/js/example_deployer.js:6
web3.setProvider(web3.providers.HttpProvider('http://localhost:8545'));
^
TypeError: Cannot read property 'HttpProvider' of undefined
at Object.<anonymous> (/home/raj/Coding/Ethereum/first_truffle_project/js/example_deployer.js:6:33)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:829:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)
我的程序有什么问题?我该如何解决这个错误?
这样试试:
web3 = new Web3(new Web3.providers.HttpProvider('node-url'))
确保您在 package.json 中的 Web 3 版本是
"web3": "^1.0.0-beta.37"
我相信你想要这样的东西。 (注意大小写。)
var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));