[英]ReferenceError with Node.js and Browserify
I try to include this build , in my browser JS with Node.js, here is my server code: 我尝试在带有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...");
and here is the content of index.html
: 这是
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. 但是,在浏览器控制台中,我收到错误
ReferenceError: Transaction is not defined
Transaction
库应该定义了Transaction
类。 So am I using browserify wrong? 那么我使用browserify错误吗?
Thanks for any help! 谢谢你的帮助!
From your question, it appears that you are not using Browserify directly; 从您的问题来看,您似乎没有直接使用Browserify; rather, you are using a UMD bundle that has been built using Browserify.
相反,您使用的是使用Browserify构建的UMD捆绑软件。
When a UMD bundle is included in a script
element, its module is exposed via a global - a property added to window
. 当
script
元素中包含UMD捆绑包时,其模块会通过全局公开-添加到window
的属性来公开。 In this case the global/property is named EthJS
. 在这种情况下,全局/属性名为
EthJS
。 If you log that using console.log(EthJS)
, you should see this: 如果使用
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
Which suggests that the transaction constructor is named Tx
, so your code should likely be: 这表明事务构造函数被命名为
Tx
,因此您的代码应为:
<script>
$(document).ready(function() {
var tx = new EthJS.Tx(...);
...
}); // document.ready
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.