node.js Socket.io 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11266608/
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
Socket.io error
提问by APalmer
I am attempting to run Nodejs with the Socket.io module. I have installed the latest version of Nodejs, and I installed socket.io from a command prompt opened as an administrator (I'm in windows 7) using the command npm install socket.io The install appears to complete without any problems but when I attempt to run the following test program:
我正在尝试使用 Socket.io 模块运行 Nodejs。我已经安装了最新版本的 Nodejs,并使用命令 npm install socket.io 从以管理员身份打开的命令提示符(我在 Windows 7 中)安装了 socket.io 安装似乎完成没有任何问题,但是当我尝试运行以下测试程序:
var io = require('socket.io').listen(80);
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
I receive this error:
我收到此错误:
module.js:340
throw err;
Error: Cannot find module 'socket.io'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (C:\xampp\htdocks\HTML5Game\impact\app.js:1:72)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function .Module._load (module.js:312:12)
at Module.runMain (module.js:487:10)
In my searching I found some things about dependency problems, and some suggestions about incompatibility between socket.io and a version of Nodejs, but both were talking about older versions of socket.io and Node Thanks.
在我的搜索中,我发现了一些关于依赖问题的事情,以及一些关于 socket.io 和 Nodejs 版本之间不兼容的建议,但两者都在谈论旧版本的 socket.io 和 Node 谢谢。
回答by Pavan Kumar Sunkara
cd app
rm -rf node_modules
npm cache clean
npm install
Explanation
解释
cd app
Go to your app directory
转到您的应用程序目录
rm -rf node_modules
Delete your currently installed modules
删除当前安装的模块
npm cache clean
Delete your npm cache, (some errors are caused by this)
删除你的 npm 缓存,(一些错误是由此引起的)
npm install
Install modules listed in your package.json. If you don't have a package.json, you can install a specific module like this
安装package.json. 如果你没有package.json,你可以像这样安装一个特定的模块
npm install <module_name>
Example
例子
npm install socket.io
In your case, if you don't know what a package.jsonis, please read up on it herebefore continuing to work on nodejs.
在你的情况下,如果你不知道 apackage.json是什么,请在继续研究 nodejs 之前在这里阅读它。
回答by Toru Yamazaki
I had to work around this problem in the following way.
我必须通过以下方式解决这个问题。
1)I put a test program in the following folder.
1)我在以下文件夹中放置了一个测试程序。
c:\program files\nodejs\node_modules
c:\程序文件\nodejs\node_modules
In this case, the source code is as follows.
在这种情况下,源代码如下。
var io = require('socket.io').listen(80);
2)I specify the full path of the socket.io.
2)我指定了socket.io的完整路径。
c:>dir /x[enter]
c:>dir /x[输入]
I then type the following command to get the "progra~1".
然后我输入以下命令来获取“ progra~1”。
var io = require("c:/progra~1/nodejs/node_modules/socket.io").listen(80);
回答by fon60
If you want use a module in more then one project or have clean project directory you can add "-g" parameter to npm command. Like so:
如果您想在多个项目中使用一个模块或拥有干净的项目目录,您可以在 npm 命令中添加“-g”参数。像这样:
npm install socket.io -g
回答by Sveti Jorjo
I've had the same problem. The only thing you need to do is to run "npm install socket.io" not in the folder where you have installed your node.js, but in the folder where you started your node server file. For example I have file server.js with code `
我遇到了同样的问题。您唯一需要做的就是运行“npm install socket.io”,而不是在您安装 node.js 的文件夹中,而是在您启动节点服务器文件的文件夹中。例如我有文件 server.js 与代码`
var io = require('socket.io').listen(
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
` just in the same folder run 'sudo npm install socket.io' and everything should be good to go.
` 只需在同一个文件夹中运行“sudo npm install socket.io”,一切都应该顺利。
回答by Justin Warkentin
For future reference, for those wondering what the real problem is, there is this two year old bug with npm that has yet to be addressed: https://github.com/isaacs/npm/issues/1341
为了将来参考,对于那些想知道真正问题是什么的人,npm 有这个两年前的错误尚未解决:https: //github.com/isaacs/npm/issues/1341
The problem is that if you have a dependency of socket.io already installed in your top-level node_modules directory then npm won't install that dependency for any modules you install that depend on it.
问题是,如果您的顶级 node_modules 目录中已经安装了 socket.io 的依赖项,那么 npm 将不会为您安装的任何依赖它的模块安装该依赖项。
All you really have to do when you run into trouble is the following (replace socket.iowith whatever module is giving you trouble):
当您遇到麻烦时,您真正需要做的就是以下内容(替换socket.io为给您带来麻烦的任何模块):
mv node_modules node_modules.bak
npm install socket.io
mv node_modules/socket.io node_modules.bak
rmdir node_modules
mv node_modules.bak node_modules
回答by Sohini
Cannot find module 'socket.io' means you don't have 'socket.io' module installed in your node modules.
找不到模块“socket.io”意味着您的节点模块中没有安装“socket.io”模块。
simply run the following command:
只需运行以下命令:
npm install socket.io
if you run:
如果你运行:
npm install socket.io --save
it will update your package.json file.
它将更新您的 package.json 文件。
回答by David Cespedes
I fix it with:
我修复它:
npm install --save socket.io
And it run!
它运行!
That's how they do it in http://socket.io/get-started/chat/

