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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:23:17  来源:igfitidea点击:

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

javascriptnode.jssocket.io

提问by test

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

所以我去 Git Bash 并输入npm install socket.io我在我的用户文件夹中找到的目录:C:\Users\weka\node_modules...无论如何,我将该socket.io文件夹拖到我的项目www文件夹中,因为我使用的是server.xml文件WAMP

So, here's my server.jsfor 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 cmdand 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 modulecalled 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 expressand I now I have expressfolder in my node_modulesfolder.

更新:我发现我没有安装它。好的,我输入了npm install express,现在我的express文件夹中有文件node_modules夹了。

回答by jmar777

expressand socket.ioare different libraries. Just npm install expressfrom the root of your app.

express并且socket.io是不同的库。只是npm install express从你的应用程序的根。

Also, make sure that your nodedependencies 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 expressmodule folders from C:\Users\weka\node_modulesto 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。它现在应该可以工作了!!!!