Installing Native Node.js Modules in Windows

July 12th 2015 Node.js C/C++ Windows

Native Node.js Modules

Installing a npm package can consist of much more than just simply downloading a bunch of files and dropping them somewhere on your local disk. The packages need not only be written in JavaScript, they can also include native Node.js modules. Since we're talking about a platform agnostic environment, the packages don't include precompiled binary files; the sources need to be compiled when the package is installed on the local computer.

Packages with native modules depend on the node-gyp toolkit for compiling the sources. Of course the toolkit by itself doesn't include all the required dependencies for compilation; there's a list of prerequisites to make it work on each of the supported platforms. In spite of that I still found it a bit challenging to make it work in Windows; in particular when I wasn't dealing with a freshly installed machine and there were different development tools already installed.

Preparing a Clean Machine

Installing Python is the easy part of the job. Just make sure you're installing version 2.7, and don't forget to select the feature for adding python.exe to path. Alternatively you can create an environment variable named PYTHON and set its value to the full path of python.exe on your computer. If you choose to install Python using Chocolatey, this is going to be taken care of automatically.

Add python.exe to Path

The troubles start with the C++ compiler tooling. At least for 64-bit Node.js installations (is anybody still using anything else?), installing Windows SDK 7.1 only turned out to be the simplest approach for clean machines. I opted for the default set of features just to be sure, although probably not everything is really needed. Unless disk space or bandwidth is an issue, I recommend you do the same. After the SDK is installed just run npm from the Windows SDK 7.1 Command Prompt or make sure you run the same SetEnv.cmd from your command prompt before using npm.

Conflicts With Existing Software

Unfortunately the SDK installation tends to fail with weird errors, if newer development tools are already installed on the machine - different versions of Visual C++ 2010 components in particular. You'll most likely need to uninstall them all for the setup to succeed. And even after it does - you'll still need to have the right C++ compiler tooling configured in your command prompt. Running Windows SDK 7.1 Command Prompt of course remains an option, but having it configured in your standard command prompt and switching between different compilers quickly becomes an issue.

As it turns out, there is another way to make node-gyp compilation work if you already have more recent C++ compiler tooling configured on your computer. By default Visual C++ 2010 compilers are used, but there is a command line switch to force a different version: msvs_version. To use Visual C++ 2013 tooling, for example, the following command does the trick:

npm install contextify --save --msvs_version=2013

The same option can be used to restore npm dependencies on a different machine:

npm install --msvs_version=2013

I like this approach a lot, since there's less additional software to install, and less possibilities to break something else in the process of making this work.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License