I am having difficulty setting up a node.js client to connect to a socket in java. the browser always give me this error
Uncaught ReferenceError: require is not defined
and i used it in my html page
<script type="text/javascript">
$(function() {
// open websocket
var net = require('net');
var socket = net.connect(8000, 'localhost');
})
</script>
i have installed node.js using git clone https://github.com/joyent/node.git
and browserify by sudo npm install -g browserify
. i used sudo
because it throws an permission error
that i should execute the command with administrator level.
after installing browserify i am still getting the error in my browser
Uncaught ReferenceError: require is not defined
i understand my error i'am trying to import an nodejs to html which is not applicable. The next thing im doing is with browserify when i try to browserify index.js -o bundle.js it shows this error
/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)
is this error with browserify? example of my 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 is my reference.
You've missed a major step in the browserify workflow. What browserify does is transform your javascript from works-in-node to works-in-the-browser, and that requires actually running the browserify
command line tool on your javascript, which will generate for you a new javascript file (the "bundle"), which you can then reference in your <script>
tag, load into the browser, and have work correctly. Go through a tutorial again and pay attention to that step.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.