Invalid JSON RPC response error

Hi, i am sending payments with a node.js script. Randomly I get this error, any help on what it can be?
thanks

Error: Invalid JSON RPC response: ""
 at Object.InvalidResponse (c:\***\web3\lib\web3\errors.js:38:16)
    at XMLHttpRequest.request.onreadystatechange (c:\***\node_modules\web3\lib\web3\httpprovider.js:125:24)
    at XMLHttpRequestEventTarget.dispatchEvent (c:\***\node_modules\xhr2\lib\xhr2.js:64:18)
    at XMLHttpRequest._setReadyState (c:\***\node_modules\xhr2\lib\xhr2.js:354:12)
    at XMLHttpRequest._onHttpRequestError (c:\***\node_modules\xhr2\lib\xhr2.js:544:12)
    at ClientRequest.<anonymous> (c:\**\xhr2\lib\xhr2.js:414:24)
    at ClientRequest.emit (events.js:189:13)
    at TLSSocket.socketErrorListener (_http_client.js:392:9)
    at TLSSocket.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)

Code:

var Tx = require('ethereumjs-tx');

for (var i = 0; i < payments.length; i++ )
{
  if ( payments[i][1] > 0 )
	{

		var rawTx = {
			"from": web3.eth.defaultAccount,
			"to": payments[i][0],
      "gasPrice": web3.toHex(web3.toWei(gasprice, 'gwei')),
      "gasLimit": web3.toHex(21000),
			"value": web3.toHex(web3.toWei(payments[i][1], 'ether')),
			"nonce": web3.toHex(nonce++),
      "chainid": config.chainid
		};

    //console.log(rawTx);
		var tx = new Tx(rawTx);
		var _privatekey = new Buffer.from(wallet.getPrivateKey(), 'hex');
		tx.sign(_privatekey);
		var serializedTx = tx.serialize();
    if ( dryrun == 'Y' )
			;
		else
			web3.eth.sendRawTransaction('0x' + serializedTx.toString('hex'), function(err, hash)
			{ 
				if (!err) {
    			// console.log("Send Tx: " + hash );
  			} else {
    			console.log(err);
  			}
			});

	  console.log('Paid ' + payments[i][1] + ' ETH to address: ' + payments[i][0]);
		totalpaid += parseFloat(payments[i][1]);
    totalfees += (gasprice * 21000);

Hi @sunchaser,

It is somewhat difficult to get detailed errors from the web3 library, but you may find this issue helpful: https://github.com/ethereum/web3.js/issues/3201.

Specifically, you might try adding the following code to the top of your file:

const XHR = require('xhr2-cookies').XMLHttpRequest
XHR.prototype._onHttpRequestError = function (request, error) {
  if (this._request !== request) {
      return;
  }
  // A new line
  console.log(error, 'request')
  this._setError();
  request.abort();
  this._setReadyState(XHR.DONE);
  this._dispatchProgress('error');
  this._dispatchProgress('loadend');
};
3 Likes

Hi Tim, problem solved thanks to your help, thanks!

You’re welcome, glad it helped!