javascript 找不到模块`express` | socket.io [node.js]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10589177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot find module `express` | socket.io [node.js]
提问by test
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.
所以我去 Git Bash 并输入npm install socket.io
我在我的用户文件夹中找到的目录:C:\Users\weka\node_modules
...无论如何,我将该socket.io
文件夹拖到我的项目www
文件夹中,因为我使用的是server.xml文件WAMP
。
So, here's my server.js
for testing:
所以,这是我server.js
的测试:
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
我进入cmd
并输入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. :\
我收到错误,它找不到被module
调用的express
. 我以为我下载了socket.io
?我是 GitHub 的新手……所以我可能做错了。:\
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.
更新:我发现我没有安装它。好的,我输入了npm install express
,现在我的express
文件夹中有文件node_modules
夹了。
回答by jmar777
express
and socket.io
are different libraries. Just npm install express
from the root of your app.
express
并且socket.io
是不同的库。只是npm install express
从你的应用程序的根。
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:
此外,请确保您的node
依赖项位于名为的文件夹中node_modules
- 这是用于模块解析的约定。因此,您应该有一个类似于以下内容的文件结构:
/some-app
/node_modules
/express
/socket.io
server.js
回答by Ali
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.
我的解决方法是在项目的根目录下运行 npm。它安装与您的项目相关的文件,这是 node.js 然后查找它们以解析文件名的方式。
回答by Rahul Gupta
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 !!!!
在你的情况,你应该复制express
的模块文件夹C:\Users\weka\node_modules
到你的项目目录为:C:\wamp\www\gameTest\node_modules
。如果您'node_modules'
的项目文件夹中没有命名文件夹,请先创建它并将这些文件粘贴到此文件夹中。这种方法在我的windows pc
. 重新启动您的节点服务器并再次运行命令node C:\wamp\www\gameTest\server.js
。它现在应该可以工作了!!!!