Node.js 和 Browserify 的 ReferenceError
ReferenceError with Node.js and Browserify
我尝试在我的浏览器 JS 中使用 Node.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
class 已定义。那么我使用 browserify 错了吗?
感谢您的帮助!
从你的问题来看,你似乎没有直接使用 Browserify;相反,您使用的是使用 Browserify 构建的 UMD 包。
当一个 UMD 包包含在一个 script
元素中时,它的模块通过全局公开 - 一个 属性 添加到 window
。在这种情况下,global/property 被命名为 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>
我尝试在我的浏览器 JS 中使用 Node.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
class 已定义。那么我使用 browserify 错了吗?
感谢您的帮助!
从你的问题来看,你似乎没有直接使用 Browserify;相反,您使用的是使用 Browserify 构建的 UMD 包。
当一个 UMD 包包含在一个 script
元素中时,它的模块通过全局公开 - 一个 属性 添加到 window
。在这种情况下,global/property 被命名为 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>