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