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`, ..., `4`, `5`, `6`,
14 etc.), regardless of what version of node is actually installed on your system
15 (`node-gyp` downloads the necessary development files or headers 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/download/)
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 * Visual C++ Build Environment:
46 * Option 1: Install [Visual C++ Build Tools](http://landinghub.visualstudio.com/visual-cpp-build-tools) using the **Default Install** option.
48 * Option 2: Install [Visual Studio 2015](https://www.visualstudio.com/products/visual-studio-community-vs) (or modify an existing installation) and select *Common Tools for Visual C++* during setup. This also works with the free Community and Express for Desktop editions.
50 > :bulb: [Windows Vista / 7 only] requires [.NET Framework 4.5.1](http://www.microsoft.com/en-us/download/details.aspx?id=40773)
52 * Install [Python 2.7](https://www.python.org/downloads/) (`v3.x.x` is not supported), and run `npm config set python python2.7` (or see below for further instructions on specifying the proper Python version and path.)
53 * Launch cmd, `npm config set msvs_version 2015`
55 If the above steps didn't work for you, please visit [Microsoft's Node.js Guidelines for Windows](https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#compiling-native-addon-modules) for additional tips.
57 If you have multiple Python versions installed, you can identify which Python
58 version `node-gyp` uses by setting the '--python' variable:
61 $ node-gyp --python /path/to/python2.7
64 If `node-gyp` is called by way of `npm` *and* you have multiple versions of
65 Python installed, then you can set `npm`'s 'python' config key to the appropriate
69 $ npm config set python /path/to/executable/python2.7
72 Note that OS X is just a flavour of Unix and so needs `python`, `make`, and C/C++.
73 An easy way to obtain these is to install XCode from Apple,
74 and then use it to install the command line tools (under Preferences -> Downloads).
79 To compile your native addon, first go to its root directory:
85 The next step is to generate the appropriate project build files for the current
86 platform. Use `configure` for that:
92 __Note__: The `configure` step looks for the `binding.gyp` file in the current
93 directory to process. See below for instructions on creating the `binding.gyp` file.
95 Now you will have either a `Makefile` (on Unix platforms) or a `vcxproj` file
96 (on Windows) in the `build/` directory. Next invoke the `build` command:
102 Now you have your compiled `.node` bindings file! The compiled bindings end up
103 in `build/Debug/` or `build/Release/`, depending on the build mode. At this point
104 you can require the `.node` file with Node and run your tests!
106 __Note:__ To create a _Debug_ build of the bindings file, pass the `--debug` (or
107 `-d`) switch when running either the `configure`, `build` or `rebuild` command.
110 The "binding.gyp" file
111 ----------------------
113 Previously when node had `node-waf` you had to write a `wscript` file. The
114 replacement for that is the `binding.gyp` file, which describes the configuration
115 to build your module in a JSON-like format. This file gets placed in the root of
116 your package, alongside the `package.json` file.
118 A barebones `gyp` file appropriate for building a node addon looks like:
124 "target_name": "binding",
125 "sources": [ "src/binding.cc" ]
131 Some additional resources for addons and writing `gyp` files:
133 * ["Going Native" a nodeschool.io tutorial](http://nodeschool.io/#goingnative)
134 * ["Hello World" node addon example](https://github.com/nodejs/node/tree/master/test/addons/hello-world)
135 * [gyp user documentation](https://gyp.gsrc.io/docs/UserDocumentation.md)
136 * [gyp input format reference](https://gyp.gsrc.io/docs/InputFormatReference.md)
137 * [*"binding.gyp" files out in the wild* wiki page](https://github.com/nodejs/node-gyp/wiki/%22binding.gyp%22-files-out-in-the-wild)
143 `node-gyp` responds to the following commands:
145 | **Command** | **Description**
146 |:--------------|:---------------------------------------------------------------
147 | `help` | Shows the help dialog
148 | `build` | Invokes `make`/`msbuild.exe` and builds the native addon
149 | `clean` | Removes the `build` directory if it exists
150 | `configure` | Generates project build files for the current platform
151 | `rebuild` | Runs `clean`, `configure` and `build` all in a row
152 | `install` | Installs node header files for the given version
153 | `list` | Lists the currently installed node header versions
154 | `remove` | Removes the node header files for the given version
160 `node-gyp` accepts the following command options:
162 | **Command** | **Description**
163 |:----------------------------------|:------------------------------------------
164 | `-j n`, `--jobs n` | Run make in parallel
165 | `--target=v6.2.1` | Node version to build for (default=process.version)
166 | `--silly`, `--loglevel=silly` | Log all progress to console
167 | `--verbose`, `--loglevel=verbose` | Log most progress to console
168 | `--silent`, `--loglevel=silent` | Don't log anything to console
169 | `debug`, `--debug` | Make Debug build (default=Release)
170 | `--release`, `--no-debug` | Make Release build
171 | `-C $dir`, `--directory=$dir` | Run command in different directory
172 | `--make=$make` | Override make command (e.g. gmake)
173 | `--thin=yes` | Enable thin static libraries
174 | `--arch=$arch` | Set target architecture (e.g. ia32)
175 | `--tarball=$path` | Get headers from a local tarball
176 | `--ensure` | Don't reinstall headers if already present
177 | `--dist-url=$url` | Download header tarball from custom URL
178 | `--proxy=$url` | Set HTTP proxy for downloading header tarball
179 | `--cafile=$cafile` | Override default CA chain (to download tarball)
180 | `--nodedir=$path` | Set the path to the node binary
181 | `--python=$path` | Set path to the python (2) binary
182 | `--msvs_version=$version` | Set Visual Studio version (win)
183 | `--solution=$solution` | Set Visual Studio Solution version (win)
191 Copyright (c) 2012 Nathan Rajlich <nathan@tootallnate.net>
193 Permission is hereby granted, free of charge, to any person obtaining
194 a copy of this software and associated documentation files (the
195 'Software'), to deal in the Software without restriction, including
196 without limitation the rights to use, copy, modify, merge, publish,
197 distribute, sublicense, and/or sell copies of the Software, and to
198 permit persons to whom the Software is furnished to do so, subject to
199 the following conditions:
201 The above copyright notice and this permission notice shall be
202 included in all copies or substantial portions of the Software.
204 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
205 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
206 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
207 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
208 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
209 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
210 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
213 [python-v2.7.10]: https://www.python.org/downloads/release/python-2710/
214 [msvc2013]: https://www.microsoft.com/en-gb/download/details.aspx?id=44914
215 [win7sdk]: https://www.microsoft.com/en-us/download/details.aspx?id=8279
216 [compiler update for the Windows SDK 7.1]: https://www.microsoft.com/en-us/download/details.aspx?id=4422