简体   繁体   English

TypeError:require(…)不是节点js中的函数

[英]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. 我正在尝试学习节点js,并尝试了一些示例代码,当我访问socket.io网站时,我在其网站上尝试了示例代码。 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. 我是Node js的新手,请帮助我。 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)

like @venogopal said, the issue might be the version of socket.io : 就像@venogopal所说,问题可能是socket.io的版本:

for 0.9.x : 对于0.9.x

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

for 1.xx : 对于1.xx

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM