9

I have been trying to use ethereumjs-tx in a browser. I have downloaded the browser builds from here https://github.com/ethereumjs/browser-builds/tree/master/dist

This is my code snippet

<head>
    <title></title>
    <meta charset="utf-8" />
    <script type="text/javascript" src="ethereumjs-tx.js"></script>
</head>
<body>

 var Tx = require('ethereumjs-tx');
    var privateKey = new Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex');

var rawTx = {
    nonce: '00',
    gasPrice: '09184e72a000',
    gasLimit: '2710',
    to: '0000000000000000000000000000000000000000',
    value: '00',
    data: '7f7465737432000000000000000000000000000000000000000000000000000000600057'
};

var tx = new Tx(rawTx);
tx.sign(privateKey);

var serializedTx = tx.serialize();

However, it keeps throwing the error in the console Uncaught ReferenceError: require is not defined

10

try this..

<html>
<head>
<!--
<script src="https://github.com/ethereumjs/browser-builds/blob/master/dist/ethereumjs-tx/ethereumjs-tx-1.3.3.min.js"></script>
-->
<script src="https://cdn.jsdelivr.net/gh/ethereumjs/browser-builds/dist/ethereumjs-tx/ethereumjs-tx-1.3.3.min.js"></script>
<script>

console.log('typeof ethereumjs:',               (typeof ethereumjs))
console.log('Object.keys(ethereumjs):',         Object.keys(ethereumjs))
console.log('typeof ethereumjs.Tx:',            (typeof ethereumjs.Tx))
console.log('typeof ethereumjs.RLP:',           (typeof ethereumjs.RLP))
console.log('typeof ethereumjs.Util:',          (typeof ethereumjs.Util))
console.log('typeof ethereumjs.Buffer:',        (typeof ethereumjs.Buffer))
console.log('typeof ethereumjs.Buffer.Buffer:', (typeof ethereumjs.Buffer.Buffer))

{
  let privateKey = new ethereumjs.Buffer.Buffer('e331b6d69882b4cb4ea581d88e0b604039a3de5967688d3dcffdd2270c0fd109', 'hex')
  let txParams = {
    nonce:    '0x00',
    gasPrice: '0x09184e72a000', 
    gasLimit: '0x2710',
    to:       '0x0000000000000000000000000000000000000000', 
    value:    '0x00', 
    data:     '0x7f7465737432000000000000000000000000000000000000000000000000000000600057'
  }
  let tx = new ethereumjs.Tx(txParams)
  tx.sign(privateKey)
  let serializedTx = tx.serialize().toString('hex')

  console.log('serializedTx:', serializedTx)
}

</script>
</head>
<body>
</body>
</html>
  • 1
    Thanks! I was facing the same issue and this answer helped. – David T Sep 17 '17 at 20:44
  • 1
    @Etherer Thank you for your answer, I tested it and it works! Is there a general approach such that i can run the JavaScript code presented HERE: link which uses ethereumjs-abi ? I described the main question HERE: link Thanks – Questioner Jul 22 '18 at 14:32
  • @Etherer Thanks for this, I got it to work. But I can only get the EthJS objects/functions with the script in your code. The new browser builds from them do not have them, do you know if this is a problem? – John Murphy Jul 27 '18 at 9:11
4

This isn't really specifically Ethereum related but whatever.

require() isn't a valid JS function client-side. Meaning...your browser doesn't know what to do when it gets to that require line.

There is a nifty package called require.js that does extend the require functionality to your browser.

I'm not familiar with their browser builds but it may be that you are using code not meant for those builds. If I were you I would try removing the "require" line and seeing if it runs, as you are loading ethereumjs-tx in the head.

Furthermore, if that is an HTML snippet, you should wrap your JS in <script> tags and probably console.log(serializedTx) or whatever you are trying to get so something so you can actually see the result when it runs. ;)

  • Thanks for your answer. However, ethereumjs is not related to Ethereum ? – Questioner Jul 22 '18 at 14:20
  • The error itself ( Uncaught ReferenceError: require ) is much more broad than Ethereum and happens all over in Javascript-land. It is not Ethereum-specific – tayvano Jul 26 '18 at 10:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.