Cannot find module `express` | socket.io [node.js]

2019-05-06 19:29发布

So I went to Git Bash and typed npm install socket.io I found the the directory in my user folder: C:\Users\weka\node_modules... anyway, I dragged the socket.io folder into my project www folder because I am using WAMP server.

So, here's my server.js for testing:

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

io.sockets.on('connection', function (socket) {
console.log('Someone connected!');
socket.on('set nickname' , function (nickname) {
socket.nickname = nickname;
console.log(nickname + ' just connected!');
});

});

app.listen(8080);

and I go into cmd and type node C:\wamp\www\gameTest\server.js

and I get the error that it cannot find the module called express. I thought I downloaded socket.io? I am a newb when it comes to GitHub.. so I probably did it wrong. :\

Help?

UPDATE: I found out I hadn't installed it. OK, I typed npm install express and I now I have express folder in my node_modules folder.

3条回答
小情绪 Triste *
2楼-- · 2019-05-06 19:48

express and socket.io are different libraries. Just npm install express from the root of your app.

Also, make sure that your node dependencies are in a folder called node_modules - that's the convention used for module resolution. So, you should have a file structure that looks something like:

/some-app
    /node_modules
        /express
        /socket.io
    server.js
查看更多
Melony?
3楼-- · 2019-05-06 20:00

In your case, you should copy the express module folders from C:\Users\weka\node_modules to your project directory as : C:\wamp\www\gameTest\node_modules. If you do not have a folder named 'node_modules' in your project folder, then create it first and paste those files into this folder. This method worked for me on my windows pc. Restart your node server and once again run the command node C:\wamp\www\gameTest\server.js. It should work now !!!!

查看更多
看我几分像从前
4楼-- · 2019-05-06 20:05

The fix for me was to run npm at the root of your project. It installs the files relative to your project which is how node.js then looks for them to resolve the filename.

查看更多
登录 后发表回答