node.js 错误:找不到模块“socket.io”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17058298/
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-09-02 14:46:00  来源:igfitidea点击:

Error: Cannot find module 'socket.io'

node.jssocket.io

提问by DmitriyB

[~]# node node.js

Error: Cannot find module 'socket.io'

[~]# node -v
v0.10.10

socket.io installed:

安装的socket.io:

npm install socket.io

npm WARN package.json [email protected] No repository field.
npm WARN package.json [email protected] 'repositories' (plural) Not supported.
npm WARN package.json Please pick one as the 'repository' field

回答by ajtrichards

Looks like you have installed socket.ioin a different location to your current path. Either install globally like below:

看起来您已安装socket.io在与当前路径不同的位置。要么像下面这样全局安装:

npm install -g socket.io

npm install -g socket.io

Or reference the location you've installed to:

或参考您已安装的位置:

var io = require('../lib/socket.io');

回答by suman1409

Thanks ajtrichards!

谢谢 ajtrichards!

Just to add to the answer - in case you simple use

只是为了添加答案 - 以防您简单使用

sudo npm install socket.io

The installation path will be

安装路径将是

/home/.../.npm/socket.io

If you use sudo npm install -g socket.io

如果你使用 sudo npm install -g socket.io

The installation path will be

安装路径将是

/usr/local/lib/node_modules/socket.io

In first case, I tried adding the socket.io path in global path variable but it did not work.

在第一种情况下,我尝试在全局路径变量中添加 socket.io 路径,但没有用。

回答by Vali S

I had the same issue with version 0.12.0 on Windows. I tried npm install -g socket.iobut that didn't change anything. Also tried npm cache cleanalso no change, but after npm update npm -g, things got well.

我在 Windows 上的 0.12.0 版也遇到了同样的问题。我试过npm install -g socket.io但这并没有改变任何东西。也试过npm cache clean也没有变化,但是在npm update npm -g 之后,事情变得很好了。

回答by ales82

This almost happens than you try to get socket.io in you html files like :

这几乎发生在您尝试在 html 文件中获取 socket.io 时,例如:

index.html

索引.html

where you have:

你有:

 < script type="text/javascript" src="/socket.io/socket.io.js"></script>

It will not find socket.iobecause you did not started module in you application file wich contain the server like

它不会找到socket.io因为您没有在包含服务器的应用程序文件中启动模块

server.js

服务器.js

You must include following lines after started your server in server.js:

server.js 中启动服务器后,您必须包含以下行:

var io = require('socket.io').listen(server);

Hope, will save time.

希望,会节省时间。

回答by S.Reddi

you might have installed but not added to the dependencies in package.json Use below command to install socket.iomodule

您可能已安装但未添加到 package.json 中的依赖项 使用以下命令安装socket.io模块

npm install socket.io --save

Hope this will resolve your problem..

希望这能解决你的问题..

回答by sumitb.mdi

I think that you have executed the command npm install socket.ioin a different location and your files are in different directory.. So either run the command in the same directory which have your files or either mention the path where you have currently installed socket.io in your PATH variable.

我认为您npm install socket.io在不同的位置执行了命令,并且您的文件在不同的目录中。所以要么在包含文件的同一目录中运行该命令,要么在 PATH 中提及当前安装 socket.io多变的。