最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: require(...) is not a function in node js - Stack Overflow

programmeradmin5浏览0评论

I am trying to learn node js and trying few sample code, and when I e 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._pile (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)

I am trying to learn node js and trying few sample code, and when I e 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._pile (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)
Share Improve this question asked Jan 11, 2016 at 6:39 Sengottaian KarthikSengottaian Karthik 3984 silver badges10 bronze badges 3
  • You need to install the package with npm. Did you install? – thefourtheye Commented Jan 11, 2016 at 6:41
  • Yeah, I have installed. – Sengottaian Karthik Commented Jan 11, 2016 at 6:44
  • 1 what's the node version? check this stackoverflow./a/33858433/1398867 – Venugopal Commented Jan 11, 2016 at 7:00
Add a ment  | 

1 Answer 1

Reset to default 6

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);
发布评论

评论列表(0)

  1. 暂无评论