Sengottaian Karthik
Sengottaian Karthik

Reputation: 398

TypeError: require(...) is not a function in node js

I am trying to learn node js and trying few sample code, and when I come across the socket.io website, I tried the sample code in their website. I don't think the code written on their website will be wrong. The error which I'm getting while I'm running the code, I don't know how to solve it, I don't know where I have done wrong. I'm very new to node js, please help me. This is the link of website which I taken the code.

And this is code which I'm trying to run.

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);

app.get('/', function(req, res){
res.sendfile('index.html');
});

io.on('connection', function(socket){
console.log('a user connected');
});

http.listen(3000, function(){
console.log('listening on *:3000');
 });

And the error I'm getting while running the code is

var io = require('socket.io')(http);                                                                                                               
                         ^                    
TypeError: require(...) is not a function                                                                                                          
at Object.<anonymous> (/home/ubuntu/workspace/index.js:3:30)                                                                                   
at Module._compile (module.js:434:26)                                                                                                          
at Object.Module._extensions..js (module.js:452:10)                                                                                            
at Module.load (module.js:355:32)                                                                                                              
at Function.Module._load (module.js:310:12)                                                                                                    
at Module.runMain [as _onTimeout] (module.js:475:10)                                                                                           
at Timer.listOnTimeout (timers.js:89:15)

Upvotes: 1

Views: 9458

Answers (1)

mido
mido

Reputation: 25054

like @venogopal said, the issue might be the version of socket.io:

for 0.9.x:

var io = require('socket.io')(http);

for 1.x.x:

var io = require('socket.io').listen(http);

Upvotes: 6

Related Questions