1

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

Error: Cannot find module 'connect'
   ...

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

应用程序.js:

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);
    });
});

和我的 package.json :

{
  "dependencies": {
    "express": "3.1.0",
    "socket.io": "*",
    "connect": "*",
    "underscore": "*"
  }
}
4

2 回答 2

4

我相信npm update会获得已安装的更新版本的模块。正如@jondlm 和@user645715 建议的那样,使用npm installornpm install -d告诉 NPM 遍历您package.json和子文件夹中的package.json任何子文件夹,将任何缺少的依赖项安装到./node_modules/. 或用于sudo npm install --global connect安装connect到全局模块文件夹,供将来的项目使用。

于 2013-10-09T19:52:10.827 回答
0

像这样 sudo npm install -g connect

它将连接安装在您正在工作的目录文件夹中,但如果您想将其链接到根文件夹,只需键入。

sudo npm 链接连接

如果仍然出现错误,请ping我。:)

于 2013-12-19T08:06:54.970 回答