我在设置node.js客户端以连接到java套接字时遇到了困难。浏览器总是给我这个错误
Uncaught ReferenceError: require is not defined
我在我的html页面中使用了它。
<script type="text/javascript">
$(function() {
// open websocket
var net = require('net');
var socket = net.connect(8000, 'localhost');
})
</script>我已经使用git clone https://github.com/joyent/node.git和sudo npm install -g browserify的browserify安装了node.js。我使用sudo是因为它抛出了一个permission error,告诉我应该以管理员级别执行命令。
安装browserify后,我的浏览器中仍然出现错误
Uncaught ReferenceError: require is not defined 我理解我的错误,我正在尝试将nodejs导入到html,这是不适用的。我做的下一件事是使用browserify,当我尝试browserify index.js -o bundle.js时,它显示以下错误
/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:906
ret = Buffer.concat(list, length);
^
TypeError: Object function Buffer(subject, encoding, offset) {
if (!(this instanceof Buffer)) {
return new Buffer(subject, encoding, offset);
}
var type;
// Are we slicing?
if (typeof offset === 'number') {
this.length = coerce(encoding);
this.parent = subject;
this.offset = offset;
} else {
// Find the length
switch (type = typeof subject) {
case 'number':
this.length = coerce(subject);
break;
case 'string':
this.length = Buffer.byteLength(subject, encoding);
break;
case 'object': // Assume object is an array
this.length = coerce(subject.length);
break;
default:
throw new Error('First argument needs to be a number, ' +
'array or string.');
}
if (this.length > Buffer.poolSize) {
// Big buffer, just alloc one.
this.parent = new SlowBuffer(this.length);
this.offset = 0;
} else {
// Small buffer.
if (!pool || pool.length - pool.used < this.length) allocPool();
this.parent = pool;
this.offset = pool.used;
pool.used += this.length;
}
// Treat array-ish objects as a byte array.
if (isArrayIsh(subject)) {
for (var i = 0; i < this.length; i++) {
this.parent[i + this.offset] = subject[i];
}
} else if (type == 'string') {
// We are a string
this.length = this.write(subject, 0, encoding);
}
}
SlowBuffer.makeFastBuffer(this.parent, this, this.offset, this.length);
} has no method 'concat'
at fromList (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:906:20)
at Transform.read (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:373:11)
at flow (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:629:52)
at Array.0 (/usr/local/lib/node_modules/browserify/node_modules/readable-stream/lib/_stream_readable.js:600:7)
at EventEmitter._tickCallback (node.js:190:38)这是browserify的错误吗?我的nodejs示例
server.js
var http = require("http");
var url = require("url");
function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;router.js
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;index.js
var server = require("./server");
var router = require("./router");
server.start(router.route);this是我的推荐人。
发布于 2014-08-29 12:46:48
您错过了browserify工作流中的一个重要步骤。browserify所做的是将javascript从节点中的工作转换为浏览器中的工作,这需要在javascript上实际运行browserify命令行工具,这将为您生成一个新的javascript文件(“捆绑包”),然后您可以在<script>标记中引用该文件,将其加载到浏览器中,并正常工作。再看一遍教程,注意这一步。
https://stackoverflow.com/questions/25561064
复制相似问题