2

when I try to run node app.js I get this error :

Error: Cannot find module 'connect'
   ...

I added connect to my package.json file, and when I run npm update , it seems to do something, but actually it doesn't, I dont know what to do , I just runed npm install express , and I still get that error. any help?

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

and my package.json :

{
  "dependencies": {
    "express": "3.1.0",
    "socket.io": "*",
    "connect": "*",
    "underscore": "*"
  }
}
7
  • Did you add var connect = require('connect');? Commented Oct 9, 2013 at 19:31
  • check /node_modules/ directory to see if it's there. If not, try npm install. If not try removing connect from the package.json and run npm install connect --save Commented Oct 9, 2013 at 19:33
  • Could you post your package.json contents, npm output, and app.js contents? That'll help identify your issue. Commented Oct 9, 2013 at 19:37
  • I just added those files contents @Jondlm Commented Oct 9, 2013 at 19:41
  • Try running npm install instead of npm update. Does it give you any errors? Commented Oct 9, 2013 at 19:43

2 Answers 2

5

I believe npm update gets newer versions of modules that are already installed. As @jondlm and @user645715 suggest, use npm install or npm install -d to tell NPM to go through your package.json and any child package.json in subfolders, installing any missing dependencies into ./node_modules/. Or use sudo npm install --global connect to install connect to a global module folder where it will be available for future projects.

Sign up to request clarification or add additional context in comments.

2 Comments

I did all that, but it stops somewhere in installing, I dont know what to do . even what to ask :(
Can you show the console trace? Does it fail when you run npm install or is that successful, and it fails when you run node.app? Another option is to delete the node_modules directory and reinstall.
0

Do like this sudo npm install -g connect

it installs connect in the directory folder where you are working,but if u want to link it to the root folder just type.

sudo npm link connect

if still the error come then ping me please.:)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.