Im following the Getting Started Tutorial on socket.io but when i came to the point of including socket.io itself, im getting an error while running the server in the console.
What am I missing?
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
Error:
/usr/share/nginx/html/server/node_modules/socket.io/lib/index.js:177
const keysIterator = this.parentNsps.keys();
^^^^^
SyntaxError: Use of const in strict mode.
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/usr/share/nginx/html/server/index.js:3:10)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
Related
I am trying to use socket.io on one of a simple project but when I run the same on my local machine I am getting this bug. I am not able to figure out what is going wrong my index.js is as follows
const app = require("express")();
const http = require("http").Server(app);
const io = require("socket.io")(http);
const port = process.env.PORT || 8080;
app.get("/", function(req, res) {
res.render("index.ejs");
});
http.listen(port, function() {
console.log("Listening on *:" + port);
});
The full log is here
Desktop/chat/node_modules/ws/lib/websocket.js:347
...options
^^^
SyntaxError: Unexpected token ...
at createScript (vm.js:74:10)
at Object.runInThisContext (vm.js:116:10)
at Module._compile (module.js:533:28)
at Object.Module._extensions..js (module.js:580:10)
at Module.load (module.js:503:32)
at tryModuleLoad (module.js:466:12)
at Function.Module._load (module.js:458:3)
at Module.require (module.js:513:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (Desktop/chat/node_modules/ws/index.js:3:19)
My node version is 8.0
This looks like it's probably the object spread operator. If you showed the actual line of code that causes the error, we could see for sure.
You will need at least node v8.6 to get support for the object spread operator by default. Starting with node v8.21, it could be enabled via command line flags.
I am trying to install Node on AWS and after installing, I am using http.createServer which is giving me the following error.
http.createServer(function (req, res) {
^
TypeError: http.createServer is not a function
at Object.<anonymous> (/home/ubuntu/test1/test1.js:4:6)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
at bootstrap_node.js:609:3
Here is the code I am typing in my test1.js
var http = require('http');
var port = 9000;
http.createServer(function(req, res){
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello there, world\n');
}).listen(port);
console.log("Listening on port " + port);
I'm trying to create a chat module using socket.io. Ideally I would like to pass the port number like this:
var anotherChatApp = require("anotherChatApp")(1337);
the problem I'm having is finding the object after I've set the listen() method.
var io = require("socket.io");
function anotherChatApp(port){
io.listen(port);
}
then I don't know where it goes, because I try to access the io object:
io.sockets.on('connection', function (socket) {
//dosomething
}
and I get runtime errors:
Cannot call method 'on' of undefined
I was loving node until I started building a module.
EDIT:
var io = require("socket.io");
function MacroChat(portNo){
io = require("socket.io").listen(portNo);
}
module.exports = MacroChat;
//here is where the errors come in
io.sockets.on('connection', function (socket){
});
And the error:
TypeError: Cannot read property 'sockets' of undefined
at Object.<anonymous> (D:\wamp\www\nodejitsu\macrochat\node_modules\MacroChat\lib\macrochat.js:10:3)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (D:\wamp\www\nodejitsu\macrochat\node_modules\MacroChat\index.js:5:18)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
I have node.js successfully installed. I have created the file
var http = require('http');
var url=require('url');
var fs=require('fs');
var io = require('socket.io');
http.createServer(function (req, res) {
fs.readFile('/var/www/nodeJS/client.html' ,
function ( err, data ) {
if ( err ) {
console.log( err );
res.writeHead(500);
return res.end( 'Error loading client.html' );
}
res.writeHead( 200 );
res.end( data );
});
}).listen(8124, '127.0.0.1');
io.listen(http);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
The error occurs whenever I use the io object. Without io it works fine.
Error: Cannot find module 'zeparser'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/var/www/nodeJS/node_modules/socket.io/node_modules/socket.io-client/node_modules/active-x-obfuscator/index.js:1:78)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.require (module.js:364:17)
After this I have also separately installed module zeparser. My default npm installation directory seems to be /usr/local/lib/node_modules.
I have set NODE_PATH as
export NODE_PATH="/usr/local/lib/node_modules"
but get the same error.
Then I tried to copy zeparser module to /var/www/nodeJS/node_modules. Then the error changes to
/var/www/nodeJS/node_modules/socket.io/lib/manager.js:104
server.on('error', function(err) {
^
TypeError: Object #<Object> has no method 'on'
at new Manager (/var/www/nodeJS/node_modules/socket.io/lib/manager.js:104:10)
at Object.exports.listen (/var/www/nodeJS/node_modules/socket.io/lib/socket.io.js:78:10)
at Object.<anonymous> (/var/www/nodeJS/app.js:35:4)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:903:3
On your root project, do:
$ sudo npm install zeparser
It resolved the problem for me.
I followed this tutorial to install the node.js on ubuntu and installation is successful. But when i type node echo-server.js i am getting following error:
module.js:340
throw err;
^
Error: Cannot find module '../../lib/ws/server'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (/home/jci/node/joyent-node-283d735/echo-server.js:2:10)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:487:10)
echo server code:
var sys = require("util")
, ws = require('../../lib/ws/server');
var server = ws.createServer({debug: true});
// Handle WebSocket Requests
server.addListener("connection", function(conn){
conn.send("Connection: "+conn.id);
conn.addListener("message", function(message){
conn.send("<"+conn.id+"> "+message);
if(message == "error"){
conn.emit("error", "test");
}
});
});
server.addListener("error", function(){
console.log(Array.prototype.join.call(arguments, ", "));
});
server.addListener("disconnected", function(conn){
server.broadcast("<"+conn.id+"> disconnected");
});
server.listen(8000);
But lib/ws/server is missing. But where do i get it from??
I don't have web socket server in my node.js installation i guess.
Thanks
Sneha