在node.js中找不到模块'connect'

时间:2013-10-09 19:26:44

标签: javascript node.js

当我尝试运行node app.js时出现此错误:

Error: Cannot find module 'connect'
   ...

我添加了连接到我的package.json文件,当我运行npm update时,它似乎做了一些事情,但事实上它没有,我不知道该怎么做,我只是毁了{{ 1}},我仍然得到那个错误。 有什么帮助吗?

app.js:

npm install express

和我的package.json:

var connect = require('connect');
var app = require('express').createServer()
var io = require('socket.io').listen(app);

app.listen(8080);

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

// usernames which are currently connected to the chat
var usernames = {};

// rooms which are currently available in chat
var rooms = ['room1','room2','room3'];

io.sockets.on('connection', function (socket) {

    // when the client emits 'adduser', this listens and executes
    socket.on('adduser', function(username){
        // store the username in the socket session for this client
        socket.username = username;
        // store the room name in the socket session for this client
        socket.room = 'room1';
        // add the client's username to the global list
        usernames[username] = username;
        // send client to room 1
        socket.join('room1');
        // echo to client they've connected
        socket.emit('updatechat', 'SERVER', 'you have connected to room1');
        // echo to room 1 that a person has connected to their room
        socket.broadcast.to('room1').emit('updatechat', 'SERVER', username + ' has connected to this room');
        socket.emit('updaterooms', rooms, 'room1');
    });

    // when the client emits 'sendchat', this listens and executes
    socket.on('sendchat', function (data) {
        // we tell the client to execute 'updatechat' with 2 parameters
        io.sockets.in(socket.room).emit('updatechat', socket.username, data);
    });

    socket.on('switchRoom', function(newroom){
        // leave the current room (stored in session)
        socket.leave(socket.room);
        // join new room, received as function parameter
        socket.join(newroom);
        socket.emit('updatechat', 'SERVER', 'you have connected to '+ newroom);
        // sent message to OLD room
        socket.broadcast.to(socket.room).emit('updatechat', 'SERVER', socket.username+' has left this room');
        // update socket session room title
        socket.room = newroom;
        socket.broadcast.to(newroom).emit('updatechat', 'SERVER', socket.username+' has joined this room');
        socket.emit('updaterooms', rooms, newroom);
    });

    // when the user disconnects.. perform this
    socket.on('disconnect', function(){
        // remove the username from global usernames list
        delete usernames[socket.username];
        // update list of users in chat, client-side
        io.sockets.emit('updateusers', usernames);
        // echo globally that this client has left
        socket.broadcast.emit('updatechat', 'SERVER', socket.username + ' has disconnected');
        socket.leave(socket.room);
    });
});

2 个答案:

答案 0 :(得分:3)

我相信npm update会获得已安装的较新版本的模块。正如@jondlm和@ user645715建议的那样,使用npm installnpm install -d告诉NPM浏览子文件夹中的package.json和任何子package.json,将任何缺少的依赖项安装到{{ 1}}。或者使用./node_modules/sudo npm install --global connect安装到全局模块文件夹中,以便将来的项目可用。

答案 1 :(得分:0)

这样做 sudo npm install -g connect

它在你工作的目录文件夹中安装connect,但如果你想将它链接到根文件夹,只需输入。

sudo npm link connect

如果仍然出现错误请点击我。:)