簡體   English   中英

Node.js和Browserify的ReferenceError

[英]ReferenceError with Node.js and Browserify

我嘗試在帶有Node.js的瀏覽器JS中包含此build ,這是我的服務器代碼:

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

這是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>

但是,在瀏覽器控制台中,我收到錯誤ReferenceError: Transaction is not defined Transaction庫應該定義了Transaction類。 那么我使用browserify錯誤嗎?

謝謝你的幫助!

從您的問題來看,您似乎沒有直接使用Browserify; 相反,您使用的是使用Browserify構建的UMD捆綁軟件。

script元素中包含UMD捆綁包時,其模塊會通過全局公開-添加到window的屬性來公開。 在這種情況下,全局/屬性名為EthJS 如果使用console.log(EthJS) ,則應該看到以下內容:

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

這表明事務構造函數被命名為Tx ,因此您的代碼應為:

<script>
$(document).ready(function() { 
    var tx = new EthJS.Tx(...);
    ...
}); // document.ready
</script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM