node.js NPM - 无法安装 socket.IO
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16469086/
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
NPM - Can't install socket.IO
提问by user2316341
I am trying to install socket.io on windows with npm for use on a nodeJS server.
我正在尝试使用 npm 在 windows 上安装 socket.io 以在 nodeJS 服务器上使用。
First, when I typed "npm install socket.IO" i had an error in the log saying something about python and node-gyp. I installed python 2.7.3 and set the environment variables.
首先,当我输入“npm install socket.IO”时,我在日志中出现了一个关于 python 和 node-gyp 的错误。我安装了 python 2.7.3 并设置了环境变量。
Now I got a new error, which has something to do with visual studio (what the hell does VS have to do with npm ? Is it about the compiler? ).
现在我遇到了一个新错误,它与visual studio 有关(VS 与 npm 有什么关系?与编译器有关?)。
The error is the same as here npm install for some packages (sqlite3, socket.io) fail with error MSB8020 on Windows 7But when I use the option in the answer instead of the error it tells me something about a possible data loss (c4267) but doesn't log any error.
该错误与此处的某些软件包(sqlite3,socket.io)的 npm install相同,在 Windows 7 上失败并显示错误 MSB8020但是当我在答案中使用该选项而不是错误时,它告诉我有关可能的数据丢失的一些信息(c4267 ) 但不记录任何错误。
Then when I start my app, it tells me cannot find module socket.io still What could this come from ?
然后当我启动我的应用程序时,它告诉我仍然找不到模块 socket.io 这可能来自什么?
Oh and also when i do npm config get root it tells me "undefined" could it have anything to do with it ? Should I install the modules globally or locally ?
哦,当我执行 npm config get root 时,它告诉我“未定义”可能与它有关吗?我应该全局安装模块还是本地安装模块?
回答by Jonathan Lonowski
At least one of the packages in Socket.IO's dependency tree is a C/C++ addonswhich needs to be compiled on your system as it's installed. And, since it's a dependency, if it doesn't succeed in installing, neither will Socket.IO.
Socket.IO 依赖树中的至少一个包是C/C++ 插件,需要在安装时在系统上编译。而且,由于它是一个依赖项,如果它没有成功安装,Socket.IO 也不会。
To enable cross-system compilation, Node.js uses node-gypas its build system. You'll need to have it installed as a global package:
为了启用跨系统编译,Node.js 使用node-gyp作为其构建系统。您需要将其安装为全局包:
npm install -g node-gyp
As well as have its dependencies installed. Abridged version:
以及安装它的依赖项。删节版:
- Python 2
- C/C++ Compiler / Build Tools
- For Windows, Microsoft Visual Studio 2013 (C++ or Windows Desktop) (Express edition)
- For 64-bit, may need Windows 7 64-bit SDK
- For Windows, Microsoft Visual Studio 2013 (C++ or Windows Desktop) (Express edition)
- 蟒蛇 2
- C/C++ 编译器/构建工具
- 对于 Windows,Microsoft Visual Studio 2013(C++ 或 Windows 桌面)(速成版)
- 对于 64 位,可能需要 Windows 7 64 位 SDK
- 对于 Windows,Microsoft Visual Studio 2013(C++ 或 Windows 桌面)(速成版)
Then, you should be able to install Socket.IO as a local package so you can requireit:
然后,您应该能够将 Socket.IO 作为本地包安装,以便您可以require:
npm install socket.io
回答by Daniel
I had a similar problem on Mac. What resolved my problem is installing a slightly older version of Socket.io.
我在 Mac 上遇到了类似的问题。解决我的问题的是安装稍旧版本的 Socket.io。
I did:
我做了:
npm install socket.io@"~0.8.1"
npm install socket.io@"~0.8.1"
which would install the latest version between 0.8.0 to 0.8.9, but not 0.9.0 or above.
这将安装 0.8.0 到 0.8.9 之间的最新版本,但不会安装 0.9.0 或更高版本。
Socket.io then installed perfectly.
Socket.io然后完美安装。
回答by Tony O'Hagan
Make sure you have all the required software to run node-gyp:
确保您拥有运行所需的所有软件node-gyp:
You can configure version of Visual Studio used by gypvia an environment variable so you can avoid having to set the --msvs_version=2012property.
您可以gyp通过环境变量配置所使用的 Visual Studio 版本,这样您就可以避免设置--msvs_version=2012属性。
Examples:
例子:
- set
GYP_MSVS_VERSION=2012for Visual Studio 2012 - set
GYP_MSVS_VERSION=2013e(the 'e' stands for 'express edition')
GYP_MSVS_VERSION=2012为 Visual Studio 2012设置- 设置
GYP_MSVS_VERSION=2013e(“e”代表“快递版”)
For the full list see - https://github.com/joyent/node/blob/v0.10.29/tools/gyp/pylib/gyp/MSVSVersion.py#L209-294
有关完整列表,请参阅 - https://github.com/joyent/node/blob/v0.10.29/tools/gyp/pylib/gyp/MSVSVersion.py#L209-294
This is still painful for Windows users of NodeJS as it assumes you have a copy of Visual Studio installed and many end users will never have this. So I'm lobbying Joyent to the encourage them to include web sockets as part of CORE node and also to possible ship a GNU gcc compiler as part of NodeJS install so we can permanently fix this problem.
这对于 NodeJS 的 Windows 用户来说仍然是痛苦的,因为它假设您已经安装了 Visual Studio 的副本,而许多最终用户永远不会拥有它。因此,我正在游说 Joyent 鼓励他们将 Web 套接字作为 CORE 节点的一部分包含在内,并可能将 GNU gcc 编译器作为 NodeJS 安装的一部分发布,以便我们可以永久解决此问题。
Feel free to add your vote at:
请随时在以下位置添加您的投票:
回答by David Tootill
The problem causing the compile failure is that the ws module installed by the engine.io module required by socket.io pulls in a backlevel version of nan. See https://github.com/BrowserSync/grunt-browser-sync/issues/95for details. To work around the problem after the build failure:
导致编译失败的问题是socket.io需要的engine.io模块安装的ws模块拉入了一个backlevel版本的nan。有关详细信息,请参阅https://github.com/BrowserSync/grunt-browser-sync/issues/95。要在构建失败后解决此问题:
- cd to node_modules/socket.io/node_modules/engine.io/node_modules/ws
- edit package.json to change the release of nan from 1.4.x to 1.6.0
- issue command node-gyp rebuild
- cd 到 node_modules/socket.io/node_modules/engine.io/node_modules/ws
- 编辑 package.json 将 nan 的版本从 1.4.x 更改为 1.6.0
- 发出命令node-gyp 重建
You should now be able to use socket.io
您现在应该可以使用 socket.io
回答by hidmouth
this problem makes me very troubled.. I tried many solutions.
这个问题让我很困扰..我尝试了很多解决方案。
I installed .NET Framework 2.0 SDK.
我安装了 .NET Framework 2.0 SDK。
I installed Python 2.7.x
我安装了 Python 2.7.x
I installed VS 2012 Express
我安装了 VS 2012 Express
I set some paths
我设置了一些路径
I executed npm install xxxwith the argument --msvs_version=2010(or 2012/2013..)...
我npm install xxx用参数执行--msvs_version=2010(or 2012/2013..)...
But all failed.
但都失败了。
finally, I uninstalled Python & .NET Framework 2.0 SDK & VS 2012, clear those paths,enable Windows Update, install all essential updates, restart my computer
最后,我卸载了 Python & .NET Framework 2.0 SDK & VS 2012,清除这些路径,启用 Windows 更新,安装所有必要的更新,重新启动我的电脑
then execute commands below:
npm install node-gyp -g
npm install socket.io -g
npm install browser-sync -g
然后执行下面的命令:
npm install node-gyp -g
npm install socket.io -g
npm install browser-sync -g
there is no errors in installation logs.
安装日志中没有错误。
Note : this solution may not work for you, but for me
注意:这个解决方案可能对你不起作用,但对我来说
回答by Tony O'Hagan
Another approach is to use Docker for Windowsand spin up a NodeJS environment. While developing you can mount your Node code as a Docker volume and so continue to update your code from Windows but execute it and install it's dependencies inside a Linux VM. When you deploy you might prefer to use a Dockerfile that COPY's your Node code into your Docker image and so bakes it into the release image you deploy.
另一种方法是使用Docker for Windows并启动 NodeJS 环境。在开发过程中,您可以将 Node 代码挂载为 Docker 卷,因此可以继续从 Windows 更新代码,但要执行它并将其依赖项安装在 Linux VM 中。当您部署时,您可能更喜欢使用 Dockerfile 将您的 Node 代码复制到您的 Docker 映像中,然后将其烘焙到您部署的发布映像中。
This approach might be required if you don't want to risk changing the socket.io version of your code or its dependencies.
如果您不想冒险更改代码的 socket.io 版本或其依赖项,则可能需要这种方法。
It also may be a valuable solution if you planned to deploy to a corporate Intranet or public/private Cloud.
如果您计划部署到企业 Intranet 或公共/私有云,它也可能是一个有价值的解决方案。
Docker can also be very handy for testing deployment under different versions of Node without disturbing the development environment of your Windows computer (e.g. for testing a NodeJS lib).
Docker 还可以非常方便地在不同版本的 Node 下测试部署,而不会干扰 Windows 计算机的开发环境(例如,用于测试 NodeJS 库)。

