0

I am able to run below code using a javascript succesfully from a command line:-

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' ]

I used the same code in reactjs, when i do npm start I get an error. For some reason it seems like the IPC connection did not work. I would very much appreciate if someone can help me on this.

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

Error from React: `

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

`

mesnia
  • 1
  • 1
  • This looks like not an issue with react but issue with net module. Can you please share index.js code. Also did you try importing: import net from 'net' instead of require? – Hemadri Dasari Aug 26 '18 at 05:31
  • This is the index.js code. – mesnia Aug 26 '18 at 06:00
  • I am trying to paste my code here and it says too long. Sorry I haven't used Stack overflow much. Let me figure this out – mesnia Aug 26 '18 at 06:06
  • Have you tried importing net as import net from 'net' instead of require? – Hemadri Dasari Aug 26 '18 at 06:16
  • Yes I did. It threw the same error. I just pasted my code in the comments below. – mesnia Aug 26 '18 at 06:18
  • What is weird to me is that I am able to connect to my private geth node using the same code if i run it via command line using `node script.js` (as in my original post above) . I am not able to figure out why else would it be different when i put the same code in reactjs index.js – mesnia Aug 26 '18 at 06:23
  • I do not have idea about web3 and net. I initially thought that require may be an issue – Hemadri Dasari Aug 26 '18 at 06:26

0 Answers0