我能够从命令行成功地使用 javascript 运行以下代码:-
var net = require('net');
var web3 = new Web3('~.ethereum/privatechain/node1/geth.ipc', net);
var version = web3.version;
console.log(version);
console.log("DEBUG: Start Executing Scxripts", version)
web3.eth.getAccounts(console.log);
$mesnia@linux:~ node interactWithNodes.js
Result:
1.0.0-beta.35
DEBUG: Start Executing Scxripts 1.0.0-beta.35
null [ '0x277F73CC484258ED27X9C8YF9A0c7dd12Ee0773b' ]
我在 reactjs 中使用了相同的代码,当我执行 npm start 时出现错误。由于某种原因,IPC 连接似乎不起作用。如果有人可以帮助我,我将不胜感激。
import React, { Component } from 'react';
import './style.css';
import Web3 from 'web3';
var net = require('net');
var web3 = new Web3(new Web3.providers.IpcProvider('/home/rjoshi/.ethereum/privatechain/node1/geth.ipc', net));
var version = web3.version;
console.log(version);
web3.eth.getAccounts(console.log);
console.log(web3.eth.coinbase);
来自反应的错误:`
index.js:37 Uncaught TypeError: net.connect is not a function
at new IpcProvider (index.js:37)
at Object../src/components/Home/index.js (index.js:7)
at __webpack_require__ (bootstrap 769a623b2fe16b225c3f:678)
at fn (bootstrap 769a623b2fe16b225c3f:88)
at Object../src/components/App/index.js (immutable.js:19)
at __webpack_require__ (bootstrap 769a623b2fe16b225c3f:678)
at fn (bootstrap 769a623b2fe16b225c3f:88)
at Object../src/index.js (index.css?f255:26)
at __webpack_require__ (bootstrap 769a623b2fe16b225c3f:678)
at fn (bootstrap 769a623b2fe16b225c3f:88)
at Object.0 (registerServiceWorker.js:117)
at __webpack_require__ (bootstrap 769a623b2fe16b225c3f:678)
at ./node_modules/ansi-regex/index.js.module.exports (bootstrap 769a623b2fe16b225c3f:724)
at bootstrap 769a623b2fe16b225c3f:724
`