Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Windows and Node: Addons

  • Loading branch information...
commit 2f3bc6afa036f2285feecf5bbd2b61d59d1c10e9 1 parent 7108a5b
Alex Young authored May 17, 2012
86  _posts/2012-05-17-windows-and-node-3.md
Source Rendered
... ...
@@ -0,0 +1,86 @@
  1
+---
  2
+layout: post
  3
+title: "Windows and Node: Addons"
  4
+author: Alex Young
  5
+categories: 
  6
+- node
  7
+- tutorials
  8
+- windows
  9
+- addons
  10
+- gyp
  11
+---
  12
+
  13
+In this article we're going to look at _addons_.  If you're a Node developer working in Windows then this article should give you enough background to at least understand the basic issues surrounding addons, and install them using npm.
  14
+
  15
+Addons are something Unix-based Node developers generally have little trouble with.  However, the history of addon support in Windows has been a little bit turbulent.  We've seen various solutions come and go -- most notably _node-waf_ and Cygwin -- but the community has now settled on [node-gyp](https://github.com/TooTallNate/node-gyp) by Nathan Rajlich.
  16
+
  17
+<div class="aside">
  18
+<strong>Hint:</strong> <a href="http://nodejs.org/docs/latest/api/all.html#all_addons">Addons</a> use C or C++ libraries.  Node modules may include both addons and JavaScript code.
  19
+</div>
  20
+
  21
+Isaac Schlueter summarised it in [this comment](http://blog.nodejs.org/2012/01/23/node-v0-7-1/#comment-1916):
  22
+
  23
+> Node-waf doesn't work on Windows.
  24
+> We're working on a solution that will make it easier to get a build toolchain in Windows based on node-gyp. Many packages will have to be updated to use this instead, so we're trying to make it as easy a transition as possible.
  25
+
  26
+###Installing a Module with Native Code
  27
+
  28
+The [fibers](https://github.com/laverdet/node-fibers) module uses node-gyp for building its native components.  However, the module's author, Marcel Laverdet, has packaged it in an extremely effective way.  Rather than force everybody to build the module, he's included binaries in the npm package.  These are not present in the GitHub repository, but are available through npm.  This includes both 32 and 64 bit versions.
  29
+
  30
+![node-fibers installation in Windows](/images/posts/win3/1_fibers.png)
  31
+
  32
+Therefore, simply running `npm install fibers` should result in a working module in Windows.  The compressed archive on npm actually includes Linux, Mac OS, and Windows binaries, for example: `bin/win32-ia32/fibers.node`.
  33
+
  34
+###Building a Module in Windows
  35
+
  36
+As an exercise, let's build the fibers module.  I picked this one because I already know it works -- although there are other modules built with node-gyp, they don't all build correctly in Windows yet.
  37
+
  38
+Building the module requires a few dependencies:
  39
+
  40
+* A global installation of node-gyp
  41
+* Python 2.7
  42
+* Microsoft Visual Studio ("Express" is free)
  43
+
  44
+####Installing node-gyp
  45
+
  46
+The node-gyp module is easy to install, but just make sure it's installed globally so it'll be in your `%PATH%`:
  47
+
  48
+{% highlight text %}
  49
+npm install -g node-gyp
  50
+{% endhighlight %}
  51
+
  52
+####Installing Python
  53
+
  54
+GYP, or Generate Your Projects, is a build system that uses Python.  [GypUserDocumentation](http://code.google.com/p/gyp/wiki/GypUserDocumentation) covers the basics, but from our perspective we just need a working version of Python 2.7.x to use it.
  55
+
  56
+I installed [Python 2.7.3](http://www.python.org/download/releases/2.7.3/) using the MSI installer.
  57
+
  58
+####Installing Microsoft Visual Studio
  59
+
  60
+Microsoft provides a free edition of Visual Studio called [Visual Studio 2010 Express](http://www.microsoft.com/visualstudio/en-us/products/2010-editions/express-iso).  I downloaded the ISO and extracted the ISO using [7-Zip](http://www.7-zip.org/).  It's 694 MB, so you might want to brew a suitable hot drink while it's downloading.
  61
+
  62
+![Installing Visual C++](/images/posts/win3/4_visualcpp_install.png)
  63
+
  64
+Fortunately, Microsoft provide this suite of tools and compilers for free with no user registration.
  65
+
  66
+[![Running Microsoft's IDE](/images/posts/win3/5_visualcpp_installed_thumb.png)](/images/posts/win3/5_visualcpp_installed_large.png)
  67
+
  68
+####Building the Module
  69
+
  70
+I downloaded the latest zip of [node-fibers](https://github.com/laverdet/node-fibers) from GitHub, opened Command Prompt, changed directory to the path of the extracted zip, then ran the following:
  71
+
  72
+{% highlight text %}
  73
+set PATH=%PATH%;C:\Python27
  74
+node-gyp configure
  75
+node-gyp build
  76
+{% endhighlight %}
  77
+
  78
+![Building the fibers module](/images/posts/win3/6_installing_node_fibers_from_source.png)
  79
+
  80
+Once that was done I ran `npm test`, and the tests all passed.  I found it quite satisfying to see Microsoft's compiler actually build something, and although it's not necessary to build the fibers module this way, there are other modules out there that don't include binaries in npm.
  81
+
  82
+###Conclusion
  83
+
  84
+Hopefully you now appreciate a little bit about how addons are build in Windows.  Although previously the situation was confusing, the community is starting to convert modules to build with node-gyp.
  85
+
  86
+I suspect there will be cases where native code can't be shipped as binaries due to licensing constraints, but it would be encouraging to see more addons include binaries for Windows.
BIN  images/posts/win3/1_fibers.png
BIN  images/posts/win3/4_visualcpp_install.png
BIN  images/posts/win3/5_visualcpp_installed_large.png
BIN  images/posts/win3/5_visualcpp_installed_thumb.png
BIN  images/posts/win3/6_installing_node_fibers_from_source.png
3  stylesheets/screen.css
@@ -3,7 +3,8 @@ pre { margin: 0; padding: 0 }
3 3
 a:link, a:visited { color: #559; }
4 4
 html body a:hover { color: #ff9900; }
5 5
 a img { border: none; }
6  
-code { background-color: #f0f4cf }
  6
+code, .aside { background-color: #f0f4cf }
  7
+.aside { padding: 10px }
7 8
 blockquote { font-style: italic; color: #666; margin-left: 40px }
8 9
 .attribution { font-style: italic; margin-left: 40px } 
9 10
 
21  tags.html
@@ -106,6 +106,10 @@ <h3 id=".net">.net</h3>
106 106
             <div>24 Feb 2010</div>
107 107
             <a href="/2010/02/24/firerainbow">FireRainbow</a>
108 108
           </li>
  109
+          <li>
  110
+            <div>17 May 2012</div>
  111
+            <a href="/2012/05/17/windows-and-node-3">Windows and Node: Addons</a>
  112
+          </li>
109 113
 </ul>      <h3 id="ajax">ajax</h3>
110 114
 <ul class="posts">          <li>
111 115
             <div>20 May 2010</div>
@@ -3253,6 +3257,11 @@ <h3 id=".net">.net</h3>
3253 3257
             <div>25 Jul 2011</div>
3254 3258
             <a href="/2011/07/25/alex-kessinger">Code Pollution</a>
3255 3259
           </li>
  3260
+</ul>      <h3 id="gyp">gyp</h3>
  3261
+<ul class="posts">          <li>
  3262
+            <div>17 May 2012</div>
  3263
+            <a href="/2012/05/17/windows-and-node-3">Windows and Node: Addons</a>
  3264
+          </li>
3256 3265
 </ul>      <h3 id="hadoop">hadoop</h3>
3257 3266
 <ul class="posts">          <li>
3258 3267
             <div>13 Jul 2011</div>
@@ -6562,6 +6571,10 @@ <h3 id=".net">.net</h3>
6562 6571
             <div>16 May 2012</div>
6563 6572
             <a href="/2012/05/16/node-roundup">Node Roundup: express-soap2json, cb, ApiServer</a>
6564 6573
           </li>
  6574
+          <li>
  6575
+            <div>17 May 2012</div>
  6576
+            <a href="/2012/05/17/windows-and-node-3">Windows and Node: Addons</a>
  6577
+          </li>
6565 6578
 </ul>      <h3 id="nodejs">nodejs</h3>
6566 6579
 <ul class="posts">          <li>
6567 6580
             <div>11 Nov 2009</div>
@@ -9250,6 +9263,10 @@ <h3 id=".net">.net</h3>
9250 9263
             <div>10 May 2012</div>
9251 9264
             <a href="/2012/05/10/windows-and-node-2">Windows and Node: Windows Performance Monitor</a>
9252 9265
           </li>
  9266
+          <li>
  9267
+            <div>17 May 2012</div>
  9268
+            <a href="/2012/05/17/windows-and-node-3">Windows and Node: Addons</a>
  9269
+          </li>
9253 9270
 </ul>      <h3 id="twitter">twitter</h3>
9254 9271
 <ul class="posts">          <li>
9255 9272
             <div>22 Jun 2010</div>
@@ -9880,6 +9897,10 @@ <h3 id=".net">.net</h3>
9880 9897
             <div>10 May 2012</div>
9881 9898
             <a href="/2012/05/10/windows-and-node-2">Windows and Node: Windows Performance Monitor</a>
9882 9899
           </li>
  9900
+          <li>
  9901
+            <div>17 May 2012</div>
  9902
+            <a href="/2012/05/17/windows-and-node-3">Windows and Node: Addons</a>
  9903
+          </li>
9883 9904
 </ul>      <h3 id="workflow">workflow</h3>
9884 9905
 <ul class="posts">          <li>
9885 9906
             <div>27 Jan 2010</div>

0 notes on commit 2f3bc6a

Please sign in to comment.
Something went wrong with that request. Please try again.