Nodegyp (or bcrypt) will not build on Windows

Nodegyp often fails to build/rebuild modules on Windows.  This happens to me every time I rebuild my Windows machine (and sometimes when I switch node versions).

Usually I attempt `npm install bcrypt` or similar and the log throws one of many errors related to node-gyp:

  • error: Can’t find “msbuild.exe”. Do you have Microsoft Visual Studio C++ 2008 installed?
  • LINK : fatal error LNK1181: cannot open input file ‘kernel32.lib’
  • msbuild.exe` failed with exit code: 1 (accompanied by something about nodegyp rebuild)
  • Failed to locate: “CL.exe”. The system cannot find the file specified.
  • and others…

Nodegyp is a way to abstract building native modules for NodeJs.   It is supposed to make things easier for cross-platform development using modules that have C or C++ code underlying them.

The problem is that Windows does not really have a simple tool chain for building native stuff.  With `nix systems, acquiring all the necessary tools is usually a single command line entry.  Windows is more a la carte due largely to the number of C++ runtimes that exist.

After a long and hideous season of pain, some folks tried to make some of this easier by creating the npm `windows-build-tools` package.  This will do quite a lot of the hard work of getting dependencies installed.  An alternative manual process is described here.

If this does not work, you may be forced to install Visual Studio 2013 Express.  This is pretty difficult to swallow for those of us who have already gone through the rather long download and install process of getting a newer version of Visual Studio running.

One thing you might try before going there is deleting your node cache.  This may seem like the nuclear option, but my experience is that dependencies can be hanging out in there, messing up the build stream.  My current guess is that this is caused by an install of Nodegyp or a Nodegyp dependency based on a different version of the C++ runtime or a different version of NodeJs.  Unfortunately doing a global uninstall of nodegyp did not fix the problem … so cache delete it was.

To blow away the cache, go to the %appdata% path (just drop that alias in your Windows Explorer path or `cd` to it in your command line), then delete the `npm` and `npm-cache` directories.

If the problem seems isolated to one project, consider blowing away node_modules and doing a fresh npm install.