0

I try to include this build, in my browser JS with Node.js, here is my server code:

var fs = require("fs");
var http = require("http");
var url = require("url");

http.createServer(function (request, response) {

    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");

    response.writeHead(200);

    if(pathname == "/") {
        html = fs.readFileSync("views/index.html", "utf8");
        response.write(html);
    } else if (pathname == "/ethereumjs-all.js") {
    script = fs.readFileSync("views/ethereumjs-all.js", "utf8");
        response.write(script);
    } 
    response.end();
}).listen(8000);

console.log("Listening to server on 8000...");

and here is the content of index.html:

<html>
  <head>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <script src="/ethereumjs-all.js"></script>
    <script>
    $(document).ready(function() { 
    var tx = new Transaction()
    ...
    }); // document.ready
    </script>
  </head>
  <body></body>
</html>

However, in the browser console, I get the error ReferenceError: Transaction is not defined The library should have Transaction class defined. So am I using browserify wrong?

Thanks for any help!

2
  • Where is defined the Transaction constructor? Commented Dec 13, 2016 at 21:41
  • @DaveGomez I'm not sure actually, but the code was working in the server side.. Now I checked ethereumjs-tx.js, line 8389 looks like the constructor, but again, I'm not very fluent in JS, so I'm not sure if this is what we need. Commented Dec 13, 2016 at 22:05

1 Answer 1

1

From your question, it appears that you are not using Browserify directly; rather, you are using a UMD bundle that has been built using Browserify.

When a UMD bundle is included in a script element, its module is exposed via a global - a property added to window. In this case the global/property is named EthJS. If you log that using console.log(EthJS), you should see this:

Object
  ABI:()
  Account: function (data)
  BN: function BN(number, base, endian)
  Block: function (data)
  Buffer: function Object
  ICAP: function Object
  RLP: function Object
  Trie: function CheckpointTrie()
  Tx: function (data)
  Units: Object
  Util: Object
  VM: function VM(trie, blockchain, opts)
  Wallet: function (priv, pub)
  WalletHD: function EthereumHDKey()
  WalletThirdparty: Object

Which suggests that the transaction constructor is named Tx, so your code should likely be:

<script>
$(document).ready(function() { 
    var tx = new EthJS.Tx(...);
    ...
}); // document.ready
</script>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much! It seems to be working. But I'm still not sure what is the difference between using browserify directly and what I'm doing. Is there an alternative way to do this? Thanks again!
You're not really using Browserify at all. Browserify combines CommonJS modules into a single bundle. You are using the pre-built ethereumjs-all.js bundle; you're not building your own. Unless you want to write your code using CommonJS modules and build your own bundle, you don't need to worry about Browserify. For the moment, you might want to stick with <script> elements and look into Browserify (or another bundler, like WebPack) when your codebase gets larger.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.