title: How to send an ERC20 token with web3js
body: <p>I'm currently using the 0.2x.x version of the Web3 JavaScript API. I deployed my custom ERC20 token by creating smart contract in solidity (on REMIX IDE).
  I installed the MetaMask and had a test on <a href="https://wallet.ethereum.org/" rel="nofollow noreferrer">https://wallet.ethereum.org/</a> to send some custom ERC token to another my account. It worked well.
  I want to add the 'send custom ERC20 Token' function in my JavaScript code using Web3js.</p>
  <p>Here is my code below.</p>
  <pre><code>var http = require('http');
  var Web3 = require('web3');
  var Tx = require('ethereumjs-tx');
  
  var abi = [{&quot;....&quot;}]; 
  var data = '0x00..';
  var contract_addr = '0x00..';
  var owner = '0x00..';
  
  var web3 = new Web3(new Web3.providers.HttpProvider(&quot;https://ropsten.infura.io/&quot;)); 
  web3.eth.getBlock(&quot;latest&quot;, (error, result) =&gt; {
  //console.log('error:', error);
  //console.log('results', result);
  });
  
  var myContract = web3.eth.contract(abi);
  var myContractInstance = myContract.at(address);
  
  eb3.eth.getDefaultAccount = owner;
  var defaultAccount = web3.eth.getDefaultAccount;
  console.log('DefaultAccount =&gt; ', defaultAccount); 
  
  var total = myContractInstance.totalSupply();
  var balance = myContractInstance.balanceOf(defaultAccount);
  
  console.log('DefulatAccount total =&gt; ',total);
  console.log('DefaultAccount balance =&gt; ',balance);
  
  var to = '0x00..';
  var isAddress = web3.isAddress(to);
  
  console.log('isAddress(to) =&gt; ',isAddress);
  console.log('balanceOf(to) =&gt; ',myContractInstance.balanceOf(to));
  </code></pre>
  <p>I'm working on the 'Ropsten Testnet' and I sent some 'ether' for test using <code>sendRawTransaction()</code>. But what I want to do is just sending my custom ERC20 token. not the ether. How can I send my own token in this script? I have no idea. I tried to use <code>sendTransaction()</code> like below.</p>
  <pre><code>var rawTx = {
      from : defaultAccount,
      nonce : nonceHex,
      gasPrice : gasPriceHex,
      gas : gasHex,
      to : to,
      value : 100,
      data : data
  };
  
  web3.eth.sendTransaction(rawTx, function(err, transactionHash) {
      if (!err)
          console.log('transactionHash =&gt; ',transactionHash);
      else
          console.log(err);
  });
  </code></pre>
  <p>I continuously received the error.</p>
  <pre><code>Error: Invalid JSON RPC response: &quot;&quot;
  at Object.InvalidResponse (/Users/rachel/dev/test/node_modules/web3/lib/web3/errors.js:38:16)
  at XMLHttpRequest.request.onreadystatechange (/Users/rachel/dev/test/node_modules/web3/lib/web3/httpprovider.js:119:24)
  at XMLHttpRequestEventTarget.dispatchEvent (/Users/rachel/dev/test/node_modules/xhr2/lib/xhr2.js:64:18)
  at XMLHttpRequest._setReadyState (/Users/rachel/dev/test/node_modules/xhr2/lib/xhr2.js:354:12)
  at XMLHttpRequest._onHttpResponseEnd (/Users/rachel/dev/test/node_modules/xhr2/lib/xhr2.js:509:12)
  at IncomingMessage.&lt;anonymous&gt; (/Users/rachel/dev/test/node_modules/xhr2/lib/xhr2.js:469:24)
  at emitNone (events.js:111:20)
  at IncomingMessage.emit (events.js:208:7)
  at endReadableNT (_stream_readable.js:1056:12)
  at _combinedTickCallback (internal/process/next_tick.js:138:11)
  </code></pre>
  <p>I really can't understand what is wrong with it. Please let me know some way to deal with this. Thanks!</p>