The following article will cover the following topics:
- The problems faced when recompiling native node modules for windows electron applications
- how to install node.js correctly on windows
- how to recompile native node modules for windows electron applications
The problems faced when recompiling native node modules for a windows electron application
Getting native modules to recompile on windows has previously been a huge pain. The first issues windows users normally face is just getting the 2010 Microsoft compiler. This compiler is only available through visual studio 2010 which isn't available from Microsoft anymore. The second issue is that node uses node-gyp as part of the recompile process. Now, node-gyp is built on top of python which means you need to also have python installed and configured correctly. The last issue is that up until recently the electron-rebuild package didn't have windows specific instructions. All these issues compound for a very unfriendly experience.
Now a few developments have made recompiling process a much more streamlined process. Microsoft has finally unbundled their 2015 and 2017 C/C++ compiler from visual studio. node-gyp finally supports the 2015 Microsoft compiler; so you don't have to go halfway around the internet to find a download link for visual studio 2010. There is also now a package called " windows-build-tools" that install the 2015 compiler and python and configures both for you.
How to install node.js correctly on windows
Before we get into recompiling you need to have node install and configured correctly.
The easy way
1. The first thing we need to do is get node and npm (nodes package manager). To get both of these go to https://nodejs.org/en/ and download version 6 of node. Do not get version 8 as it's not compatible.
2. Once nodes installer has downloaded open it and follow the wizard. Make sure that you check the "add to path" option.
3. After node has installed open PowerShell and run the following command
npm install --global --production windows-build-tools
To install the 2015 compiler, python and to auto-configure both.
The harder way
1. The first thing we need to do is install the 2015 compiler which can be downloaded at http://landinghub.visualstudio.com/visual-cpp-build-tools
2. Once downloaded open it, choose Custom Install and make sure to check both the Windows 8.1 and Windows 10 SDK options.
3. Once the 2015 compiler is installed we can now install python. Go to https://www.python.org/downloads/ and download python 2.7. Do not download version 3 as software that is written in python 2 is not compatible with version 2.
4. Once downloaded install python. Note that the "add to path" option should already be check.
5. Once python is installed we can now install and configure node by first going to https://nodejs.org/en/ and downloading version 6 of node. Do not get version 8 as it's not compatible.
6. Once nodes installer has downloaded open it and follow the wizard. Make sure that you check the "add to path" option.
7. With node installed we now need to tell it where the compiler and python are by running the following commands in PowerShell
npm config set python python2.7
npm config set msvs_version 2015 –global
Now you should be all set with a correctly configured node install.
How recompile native node modules for windows electron applications
The following instructions are written as an example for installing sqlite3 which is a native node module written in C. You should be able to repurpose these instructions for installing other native node modules as well.
1. Install the sqlite3 package using npm
npm install sqlite3 –save
2. We need to add the electron-rebuild tools to the sqlite3 module which is done through the following commands in PowerShell
npm install --save-dev electron-rebuild
3. Once the rebuild tools are added we can run the recompile command in PowerShell
If everything went all right then the sqlite3 module should have recompiled
Zach Radloff lives on the Gold Coast. He is studying IT and Multimedia at university and is a qualified Live Production, Theatre and Events Technician.
Zach Radloff's previous articles may be viewed at www.pressserviceinternational.org/zach-radloff.html