18

I am trying to install socket.io on windows with npm for use on a nodeJS server.

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.

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? ).

The error is the same as here npm install for some packages (sqlite3, socket.io) fail with error MSB8020 on Windows 7 But 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.

Then when I start my app, it tells me cannot find module socket.io still What could this come from ?

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 ?

Community
  • 1
  • 1
user2316341
  • 365
  • 4
  • 6
  • 13

6 Answers6

35

At least one of the packages in Socket.IO's dependency tree is a C/C++ addons which 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.

To enable cross-system compilation, Node.js uses node-gyp as its build system. You'll need to have it installed as a global package:

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

Then, you should be able to install Socket.IO as a local package so you can require it:

npm install socket.io
Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • Thank you for this very complete answer. I'm installing the whole thing. Tell me, if I have Visual Studio 2012 full version installed, do I need to install Visual c++ and win sdk as well? – user2316341 May 09 '13 at 22:38
  • @user2316341 Recent versions of `node-gyp` should work with VS 2012 Professional and up, assuming it was installed with features for C++ development. – Jonathan Lonowski May 09 '13 at 22:59
  • I couldn't get it to work, so I uninstalled VS 2012, I installed VS2010, the win7 64 bit sdk from your link, but I can't install the compiler update for sdk 7.1 because it tells me that I don't have it. when i google for win sdk 7.1 i find up this link http://www.microsoft.com/en-us/download/details.aspx?id=8442 I tried the first one but the install doesn't work because it's missing some files. Which one should I take ? – user2316341 May 10 '13 at 09:07
  • 3
    Also look into this answer http://stackoverflow.com/a/16469222/73630 If you have 2012 express desktop you have use command "npm install socket.io --msvs_version=2012" – Palani Mar 14 '14 at 17:43
15

I had a similar problem on Mac. What resolved my problem is installing a slightly older version of Socket.io.

I did:

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.

Socket.io then installed perfectly.

Daniel
  • 1,352
  • 1
  • 14
  • 16
7

Make sure you have all the required software to run node-gyp:

You can configure version of Visual Studio used by gyp via an environment variable so you can avoid having to set the --msvs_version=2012 property.

Examples:

  • set GYP_MSVS_VERSION=2012 for Visual Studio 2012
  • set GYP_MSVS_VERSION=2013e (the 'e' stands for 'express edition')

For the full list see - 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.

Feel free to add your vote at:

Tony O'Hagan
  • 21,638
  • 3
  • 67
  • 78
  • I tested installing this with socket.io 1.0.6 that relies on ws 0.4.31 (the bit that needs to be compiled) and Visual Studio 2012. It shows warning (yellow text) but works ok. – Tony O'Hagan Aug 01 '14 at 00:42
  • No answer should be complete without the GYP_MSVS_VERSION note. I tried everything previously mentioned and nothing worked until I set that environment variable. – ryan1234 Jan 12 '15 at 18:44
  • Agreed - This is very confusing and changes often on the Windows side. It has been a nightmare trying to keep this working. Nobody wants to fix node-gyp because its getting deprecated in favor of ninja, there doesn't seem to be any sort of path to getting around this except avoiding all packages that use native dependencies... – cchamberlain Jul 14 '15 at 00:46
0

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/95 for details. To work around the problem after the build failure:

  • 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

You should now be able to use socket.io

David Tootill
  • 541
  • 5
  • 11
0

this problem makes me very troubled.. I tried many solutions.

I installed .NET Framework 2.0 SDK.

I installed Python 2.7.x

I installed VS 2012 Express

I set some paths

I executed npm install xxx with the argument --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

then execute commands below: 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

hidmouth
  • 1
  • 2
0

Another approach is to use Docker for Windows and 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.

This approach might be required if you don't want to risk changing the socket.io version of your code or its dependencies.

It also may be a valuable solution if you planned to deploy to a corporate Intranet or public/private Cloud.

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).

Tony O'Hagan
  • 21,638
  • 3
  • 67
  • 78