1# `node-gyp` - Node.js native addon build tool 2 3[](https://github.com/nodejs/node-gyp/actions?query=workflow%3ATests+branch%3Amain) 4 5 6`node-gyp` is a cross-platform command-line tool written in Node.js for 7compiling native addon modules for Node.js. It contains a vendored copy of the 8[gyp-next](https://github.com/nodejs/gyp-next) project that was previously used 9by the Chromium team and extended to support the development of Node.js native 10addons. 11 12Note that `node-gyp` is _not_ used to build Node.js itself. 13 14Multiple target versions of Node.js are supported (i.e. `0.8`, ..., `4`, `5`, `6`, 15etc.), regardless of what version of Node.js is actually installed on your system 16(`node-gyp` downloads the necessary development files or headers for the target version). 17 18## Features 19 20 * The same build commands work on any of the supported platforms 21 * Supports the targeting of different versions of Node.js 22 23## Installation 24 25You can install `node-gyp` using `npm`: 26 27``` bash 28npm install -g node-gyp 29``` 30 31Depending on your operating system, you will need to install: 32 33### On Unix 34 35 * [A supported version of Python](https://devguide.python.org/versions/) 36 * `make` 37 * A proper C/C++ compiler toolchain, like [GCC](https://gcc.gnu.org) 38 39### On macOS 40 41 * [A supported version of Python](https://devguide.python.org/versions/) 42 * `Xcode Command Line Tools` which will install `clang`, `clang++`, and `make`. 43 * Install the `Xcode Command Line Tools` standalone by running `xcode-select --install`. -- OR -- 44 * Alternatively, if you already have the [full Xcode installed](https://developer.apple.com/xcode/download/), you can install the Command Line Tools under the menu `Xcode -> Open Developer Tool -> More Developer Tools...`. 45 46 47### On Windows 48 49Install the current [version of Python](https://devguide.python.org/versions/) from the 50[Microsoft Store](https://apps.microsoft.com/store/search?publisher=Python+Software+Foundation). 51 52Install tools and configuration manually: 53 * Install Visual C++ Build Environment: [Visual Studio Build Tools](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools) 54 (using "Visual C++ build tools" if using a version older than VS2019, otherwise use "Desktop development with C++" workload) or [Visual Studio Community](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community) 55 (using the "Desktop development with C++" workload) 56 57 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. 58 59 To target native ARM64 Node.js on Windows on ARM, add the components "Visual C++ compilers and libraries for ARM64" and "Visual C++ ATL for ARM64". 60 61 To use the native ARM64 C++ compiler on Windows on ARM, ensure that you have Visual Studio 2022 [17.4 or later](https://devblogs.microsoft.com/visualstudio/arm64-visual-studio-is-officially-here/) installed. 62 63### Configuring Python Dependency 64 65`node-gyp` requires that you have installed a [supported version of Python](https://devguide.python.org/versions/). 66If you have multiple versions of Python installed, you can identify which version 67`node-gyp` should use in one of the following ways: 68 691. by setting the `--python` command-line option, e.g.: 70 71``` bash 72node-gyp <command> --python /path/to/executable/python 73``` 74 752. If `node-gyp` is called by way of `npm`, *and* you have multiple versions of 76Python installed, then you can set the `npm_config_python` environment variable 77to the appropriate path: 78``` bash 79export npm_config_python=/path/to/executable/python 80``` 81 Or on Windows: 82```console 83py --list-paths # To see the installed Python versions 84set npm_config_python=C:\path\to\python.exe 85``` 86 873. If the `PYTHON` environment variable is set to the path of a Python executable, 88then that version will be used if it is a supported version. 89 904. If the `NODE_GYP_FORCE_PYTHON` environment variable is set to the path of a 91Python executable, it will be used instead of any of the other configured or 92built-in Python search paths. If it's not a compatible version, no further 93searching will be done. 94 95### Build for Third Party Node.js Runtimes 96 97When building modules for third-party Node.js runtimes like Electron, which have 98different build configurations from the official Node.js distribution, you 99should use `--dist-url` or `--nodedir` flags to specify the headers of the 100runtime to build for. 101 102Also when `--dist-url` or `--nodedir` flags are passed, node-gyp will use the 103`config.gypi` shipped in the headers distribution to generate build 104configurations, which is different from the default mode that would use the 105`process.config` object of the running Node.js instance. 106 107Some old versions of Electron shipped malformed `config.gypi` in their headers 108distributions, and you might need to pass `--force-process-config` to node-gyp 109to work around configuration errors. 110 111## How to Use 112 113To compile your native addon first go to its root directory: 114 115``` bash 116cd my_node_addon 117``` 118 119The next step is to generate the appropriate project build files for the current 120platform. Use `configure` for that: 121 122``` bash 123node-gyp configure 124``` 125 126Auto-detection fails for Visual C++ Build Tools 2015, so `--msvs_version=2015` 127needs to be added (not needed when run by npm as configured above): 128``` bash 129node-gyp configure --msvs_version=2015 130``` 131 132__Note__: The `configure` step looks for a `binding.gyp` file in the current 133directory to process. See below for instructions on creating a `binding.gyp` file. 134 135Now you will have either a `Makefile` (on Unix platforms) or a `vcxproj` file 136(on Windows) in the `build/` directory. Next, invoke the `build` command: 137 138``` bash 139node-gyp build 140``` 141 142Now you have your compiled `.node` bindings file! The compiled bindings end up 143in `build/Debug/` or `build/Release/`, depending on the build mode. At this point, 144you can require the `.node` file with Node.js and run your tests! 145 146__Note:__ To create a _Debug_ build of the bindings file, pass the `--debug` (or 147`-d`) switch when running either the `configure`, `build` or `rebuild` commands. 148 149## The `binding.gyp` file 150 151A `binding.gyp` file describes the configuration to build your module, in a 152JSON-like format. This file gets placed in the root of your package, alongside 153`package.json`. 154 155A barebones `gyp` file appropriate for building a Node.js addon could look like: 156 157```python 158{ 159 "targets": [ 160 { 161 "target_name": "binding", 162 "sources": [ "src/binding.cc" ] 163 } 164 ] 165} 166``` 167 168## Further reading 169 170The **[docs](./docs/)** directory contains additional documentation on specific node-gyp topics that may be useful if you are experiencing problems installing or building addons using node-gyp. 171 172Some additional resources for Node.js native addons and writing `gyp` configuration files: 173 174 * ["Going Native" a nodeschool.io tutorial](http://nodeschool.io/#goingnative) 175 * ["Hello World" node addon example](https://github.com/nodejs/node/tree/main/test/addons/hello-world) 176 * [gyp user documentation](https://gyp.gsrc.io/docs/UserDocumentation.md) 177 * [gyp input format reference](https://gyp.gsrc.io/docs/InputFormatReference.md) 178 * [*"binding.gyp" files out in the wild* wiki page](./docs/binding.gyp-files-in-the-wild.md) 179 180## Commands 181 182`node-gyp` responds to the following commands: 183 184| **Command** | **Description** 185|:--------------|:--------------------------------------------------------------- 186| `help` | Shows the help dialog 187| `build` | Invokes `make`/`msbuild.exe` and builds the native addon 188| `clean` | Removes the `build` directory if it exists 189| `configure` | Generates project build files for the current platform 190| `rebuild` | Runs `clean`, `configure` and `build` all in a row 191| `install` | Installs Node.js header files for the given version 192| `list` | Lists the currently installed Node.js header versions 193| `remove` | Removes the Node.js header files for the given version 194 195 196## Command Options 197 198`node-gyp` accepts the following command options: 199 200| **Command** | **Description** 201|:----------------------------------|:------------------------------------------ 202| `-j n`, `--jobs n` | Run `make` in parallel. The value `max` will use all available CPU cores 203| `--target=v6.2.1` | Node.js version to build for (default is `process.version`) 204| `--silly`, `--loglevel=silly` | Log all progress to console 205| `--verbose`, `--loglevel=verbose` | Log most progress to console 206| `--silent`, `--loglevel=silent` | Don't log anything to console 207| `debug`, `--debug` | Make Debug build (default is `Release`) 208| `--release`, `--no-debug` | Make Release build 209| `-C $dir`, `--directory=$dir` | Run command in different directory 210| `--make=$make` | Override `make` command (e.g. `gmake`) 211| `--thin=yes` | Enable thin static libraries 212| `--arch=$arch` | Set target architecture (e.g. ia32) 213| `--tarball=$path` | Get headers from a local tarball 214| `--devdir=$path` | SDK download directory (default is OS cache directory) 215| `--ensure` | Don't reinstall headers if already present 216| `--dist-url=$url` | Download header tarball from custom URL 217| `--proxy=$url` | Set HTTP(S) proxy for downloading header tarball 218| `--noproxy=$urls` | Set urls to ignore proxies when downloading header tarball 219| `--cafile=$cafile` | Override default CA chain (to download tarball) 220| `--nodedir=$path` | Set the path to the node source code 221| `--python=$path` | Set path to the Python binary 222| `--msvs_version=$version` | Set Visual Studio version (Windows only) 223| `--solution=$solution` | Set Visual Studio Solution version (Windows only) 224| `--force-process-config` | Force using runtime's `process.config` object to generate `config.gypi` file 225 226## Configuration 227 228### Environment variables 229 230Use the form `npm_config_OPTION_NAME` for any of the command options listed 231above (dashes in option names should be replaced by underscores). 232 233For example, to set `devdir` equal to `/tmp/.gyp`, you would: 234 235Run this on Unix: 236 237```bash 238export npm_config_devdir=/tmp/.gyp 239``` 240 241Or this on Windows: 242 243```console 244set npm_config_devdir=c:\temp\.gyp 245``` 246 247### `npm` configuration for npm versions before v9 248 249Use the form `OPTION_NAME` for any of the command options listed above. 250 251For example, to set `devdir` equal to `/tmp/.gyp`, you would run: 252 253```bash 254npm config set [--global] devdir /tmp/.gyp 255``` 256 257**Note:** Configuration set via `npm` will only be used when `node-gyp` 258is run via `npm`, not when `node-gyp` is run directly. 259 260## License 261 262`node-gyp` is available under the MIT license. See the [LICENSE 263file](LICENSE) for details. 264