3 ### Node.js native addon build tool
5 `node-gyp` is a cross-platform command-line tool written in Node.js for compiling
6 native addon modules for Node.js. It bundles the [gyp](https://code.google.com/p/gyp/)
7 project used by the Chromium team and takes away the pain of dealing with the
8 various differences in build platforms. It is the replacement to the `node-waf`
9 program which is removed for node `v0.8`. If you have a native addon for node that
10 still has a `wscript` file, then you should definitely add a `binding.gyp` file
11 to support the latest versions of node.
13 Multiple target versions of node are supported (i.e. `0.8`, `0.9`, `0.10`, ..., `1.0`,
14 etc.), regardless of what version of node is actually installed on your system
15 (`node-gyp` downloads the necessary development files for the target version).
19 * Easy to use, consistent interface
20 * Same commands to build your module on every platform
21 * Supports multiple target versions of Node
27 You can install with `npm`:
30 $ npm install -g node-gyp
33 You will also need to install:
36 * `python` (`v2.7` recommended, `v3.x.x` is __*not*__ supported)
38 * A proper C/C++ compiler toolchain, like [GCC](https://gcc.gnu.org)
40 * `python` (`v2.7` recommended, `v3.x.x` is __*not*__ supported) (already installed on Mac OS X)
41 * [Xcode](https://developer.apple.com/xcode/downloads/)
42 * You also need to install the `Command Line Tools` via Xcode. You can find this under the menu `Xcode -> Preferences -> Downloads`
43 * This step will install `gcc` and the related toolchain containing `make`
45 * [Python][windows-python] ([`v2.7.3`][windows-python-v2.7.3] recommended, `v3.x.x` is __*not*__ supported)
46 * Make sure that you have a PYTHON environment variable, and it is set to drive:\path\to\python.exe not to a folder
48 * Microsoft Visual Studio C++ 2013 ([Express][msvc2013] version works well)
49 * If the install fails, try uninstalling any C++ 2010 x64&x86 Redistributable that you have installed first
50 * If you get errors that the 64-bit compilers are not installed you may also need the [compiler update for the Windows SDK 7.1]
52 * Microsoft Visual Studio C++ 2013 for Windows Desktop ([Express][msvc2013] version works well)
53 * All Windows Versions
54 * For 64-bit builds of node and native modules you will _**also**_ need the [Windows 7 64-bit SDK][win7sdk]
55 * You may need to run one of the following commands if your build complains about WindowsSDKDir not being set, and you are sure you have already installed the SDK:
58 call "C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\Setenv.cmd" /Release /x86
59 call "C:\Program Files\Microsoft SDKs\Windows\v7.1\bin\Setenv.cmd" /Release /x64
62 If you have multiple Python versions installed, you can identify which Python
63 version `node-gyp` uses by setting the '--python' variable:
66 $ node-gyp --python /path/to/python2.7
69 If `node-gyp` is called by way of `npm` *and* you have multiple versions of
70 Python installed, then you can set `npm`'s 'python' config key to the appropriate
74 $ npm config set python /path/to/executable/python2.7
77 Note that OS X is just a flavour of Unix and so needs `python`, `make`, and C/C++.
78 An easy way to obtain these is to install XCode from Apple,
79 and then use it to install the command line tools (under Preferences -> Downloads).
84 To compile your native addon, first go to its root directory:
90 The next step is to generate the appropriate project build files for the current
91 platform. Use `configure` for that:
97 __Note__: The `configure` step looks for the `binding.gyp` file in the current
98 directory to process. See below for instructions on creating the `binding.gyp` file.
100 Now you will have either a `Makefile` (on Unix platforms) or a `vcxproj` file
101 (on Windows) in the `build/` directory. Next invoke the `build` command:
107 Now you have your compiled `.node` bindings file! The compiled bindings end up
108 in `build/Debug/` or `build/Release/`, depending on the build mode. At this point
109 you can require the `.node` file with Node and run your tests!
111 __Note:__ To create a _Debug_ build of the bindings file, pass the `--debug` (or
112 `-d`) switch when running either the `configure`, `build` or `rebuild` command.
115 The "binding.gyp" file
116 ----------------------
118 Previously when node had `node-waf` you had to write a `wscript` file. The
119 replacement for that is the `binding.gyp` file, which describes the configuration
120 to build your module in a JSON-like format. This file gets placed in the root of
121 your package, alongside the `package.json` file.
123 A barebones `gyp` file appropriate for building a node addon looks like:
129 "target_name": "binding",
130 "sources": [ "src/binding.cc" ]
136 Some additional resources for addons and writing `gyp` files:
138 * ["Going Native" a nodeschool.io tutorial](http://nodeschool.io/#goingnative)
139 * ["Hello World" node addon example](https://github.com/joyent/node/tree/master/test/addons/hello-world)
140 * [gyp user documentation](https://chromium.googlesource.com/external/gyp/+/master/docs/UserDocumentation.md)
141 * [gyp input format reference](https://chromium.googlesource.com/external/gyp/+/master/docs/InputFormatReference.md)
142 * [*"binding.gyp" files out in the wild* wiki page](https://github.com/nodejs/node-gyp/wiki/%22binding.gyp%22-files-out-in-the-wild)
148 `node-gyp` responds to the following commands:
150 | **Command** | **Description**
151 |:--------------|:---------------------------------------------------------------
152 | `build` | Invokes `make`/`msbuild.exe` and builds the native addon
153 | `clean` | Removes the `build` directory if it exists
154 | `configure` | Generates project build files for the current platform
155 | `rebuild` | Runs `clean`, `configure` and `build` all in a row
156 | `install` | Installs node development header files for the given version
157 | `list` | Lists the currently installed node development file versions
158 | `remove` | Removes the node development header files for the given version
166 Copyright (c) 2012 Nathan Rajlich <nathan@tootallnate.net>
168 Permission is hereby granted, free of charge, to any person obtaining
169 a copy of this software and associated documentation files (the
170 'Software'), to deal in the Software without restriction, including
171 without limitation the rights to use, copy, modify, merge, publish,
172 distribute, sublicense, and/or sell copies of the Software, and to
173 permit persons to whom the Software is furnished to do so, subject to
174 the following conditions:
176 The above copyright notice and this permission notice shall be
177 included in all copies or substantial portions of the Software.
179 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
180 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
181 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
182 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
183 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
184 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
185 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
188 [windows-python]: http://www.python.org/getit/windows
189 [windows-python-v2.7.3]: http://www.python.org/download/releases/2.7.3#download
190 [msvc2013]: http://www.visualstudio.com/en-us/downloads/download-visual-studio-vs
191 [win7sdk]: http://www.microsoft.com/en-us/download/details.aspx?id=8279
192 [compiler update for the Windows SDK 7.1]: http://www.microsoft.com/en-us/download/details.aspx?id=4422