2013-10-09 73 views
0

,当我尝试运行node app.js我得到这个错误:找不到模块中的Node.js“连接”

Error: Cannot find module 'connect' 
    ... 

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

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

和我的package.json:

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

你加'VAR连接=需要( '连接');'? – prototype

+0

检查'/ node_modules /'目录,看看它是否在那里。如果没有,请尝试'npm install'。如果不尝试从package.json中删除连接并运行'npm install connect --save' – prototype

+0

你可以发布你的'package.json'内容,npm输出和'app.js'内容吗?这将有助于确定您的问题。 – Jondlm

回答

3

我相信npm update获得已安装模块的新版本。正如@jondlm和@ user645715建议的那样,使用npm installnpm install -d来告诉NPM在子文件夹中通过您的package.json和任何子package.json,将任何缺少的依赖关系安装到./node_modules/中。或者使用sudo npm install --global connectconnect安装到全局模块文件夹中,以供未来的项目使用。

+0

我做了所有这些,但它停在某处安装,我不知道该怎么做。甚至要问什么:( –

+0

)你可以显示控制台跟踪吗?当你运行'npm install'时是否失败,或者是否成功,当你运行node.app时会失败?另一个选项是删除'node_modules'目录和重新安装。 – prototype

0

做这样 须藤NPM安装-g连接

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

须藤NPM链路连接

如果还是错误来然后平我请:)