2019-07-24 23:00:03 -07:00
# `node-gyp` - Node.js native addon build tool
2012-03-14 16:39:15 -07:00
2023-11-03 07:03:57 -07:00
[](https://github.com/nodejs/node-gyp/actions?query=workflow%3ATests+branch%3Amain)
2021-10-07 20:21:11 +00:00

2020-03-19 12:53:27 -04:00
2019-10-08 08:57:31 -07:00
`node-gyp` is a cross-platform command-line tool written in Node.js for
2020-10-02 17:52:19 -04:00
compiling native addon modules for Node.js. It contains a vendored copy of the
[gyp-next ](https://github.com/nodejs/gyp-next ) project that was previously used
2023-11-03 07:03:57 -07:00
by the Chromium team and extended to support the development of Node.js native
addons.
2019-10-08 08:57:31 -07:00
Note that `node-gyp` is _not_ used to build Node.js itself.
2012-03-14 16:39:15 -07:00
2024-04-07 14:36:14 -07:00
All current and LTS target versions of Node.js are supported. Depending on what version of Node.js is actually installed on your system
`node-gyp` downloads the necessary development files or headers for the target version. List of stable Node.js versions can be found on [Node.js website ](https://nodejs.org/en/about/previous-releases ).
2012-03-14 16:39:15 -07:00
2018-07-18 13:55:52 -07:00
## Features
2012-03-14 16:39:15 -07:00
2020-01-28 17:14:59 -05:00
* The same build commands work on any of the supported platforms
2020-03-19 12:53:27 -04:00
* Supports the targeting of different versions of Node.js
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
## Installation
2012-03-14 16:39:15 -07:00
2024-09-07 23:09:40 -07:00
> [!Important]
> Python >= v3.12 requires `node-gyp` >= v10
2020-01-28 17:14:59 -05:00
You can install `node-gyp` using `npm` :
2012-03-14 16:39:15 -07:00
``` bash
2021-10-07 20:21:11 +00:00
npm install -g node-gyp
2012-03-14 16:39:15 -07:00
```
2020-01-28 17:14:59 -05:00
Depending on your operating system, you will need to install:
2019-10-08 08:57:31 -07:00
2018-07-18 13:55:52 -07:00
### On Unix
2015-06-04 19:04:48 -07:00
2023-11-03 07:03:57 -07:00
* [A supported version of Python ](https://devguide.python.org/versions/ )
2018-07-18 13:55:52 -07:00
* `make`
* A proper C/C++ compiler toolchain, like [GCC ](https://gcc.gnu.org )
2016-09-22 07:59:37 -07:00
2018-07-18 13:55:52 -07:00
### On macOS
2016-09-22 07:59:37 -07:00
2023-11-03 07:03:57 -07:00
* [A supported version of Python ](https://devguide.python.org/versions/ )
* `Xcode Command Line Tools` which will install `clang` , `clang++` , and `make` .
* Install the `Xcode Command Line Tools` standalone by running `xcode-select --install` . -- OR --
2022-03-03 21:38:08 +00:00
* 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...` .
2016-09-22 07:59:37 -07:00
2018-07-18 13:55:52 -07:00
### On Windows
2024-09-07 23:09:40 -07:00
Install tools with [Chocolatey ](https://chocolatey.org ):
``` bash
choco install python visualstudio2022-workload-vctools -y
```
Or install and configure Python and Visual Studio tools manually:
* Install the current [version of Python ](https://devguide.python.org/versions/ ) from the
[Microsoft Store ](https://apps.microsoft.com/store/search?publisher=Python+Software+Foundation ).
2019-10-08 08:57:31 -07:00
2024-04-07 14:36:14 -07:00
* Install Visual C++ Build Environment: For Visual Studio 2019 or later, use the `Desktop development with C++` workload from [Visual Studio Community ](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=Community ). For a version older than Visual Studio 2019, install [Visual Studio Build Tools ](https://visualstudio.microsoft.com/thank-you-downloading-visual-studio/?sku=BuildTools ) with the `Visual C++ build tools` option.
2018-07-18 13:55:52 -07:00
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.
2012-03-14 16:39:15 -07:00
2023-06-22 07:48:43 -07:00
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".
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.
2019-07-24 23:00:03 -07:00
2024-04-07 14:36:14 -07:00
It's advised to install following Powershell module: [VSSetup ](https://github.com/microsoft/vssetup.powershell ) using `Install-Module VSSetup -Scope CurrentUser` .
This will make Visual Studio detection logic to use more flexible and accessible method, avoiding Powershell's `ConstrainedLanguage` mode.
2019-07-24 23:00:03 -07:00
### Configuring Python Dependency
2023-11-03 07:03:57 -07:00
`node-gyp` requires that you have installed a [supported version of Python ](https://devguide.python.org/versions/ ).
If you have multiple versions of Python installed, you can identify which version
`node-gyp` should use in one of the following ways:
2020-01-28 17:14:59 -05:00
1. by setting the `--python` command-line option, e.g.:
2014-06-05 15:18:15 -07:00
``` bash
2021-10-07 20:21:11 +00:00
node-gyp < command > --python /path/to/executable/python
2014-06-05 15:18:15 -07:00
```
2020-01-28 17:14:59 -05:00
2. If `node-gyp` is called by way of `npm` , *and* you have multiple versions of
2023-11-03 07:03:57 -07:00
Python installed, then you can set the `npm_config_python` environment variable
to the appropriate path:
2014-06-05 15:18:15 -07:00
``` bash
2023-11-03 07:03:57 -07:00
export npm_config_python=/path/to/executable/python
```
Or on Windows:
```console
py --list-paths # To see the installed Python versions
2024-09-07 23:09:40 -07:00
set npm_config_python=C:\path\to\python.exe # CMD
$Env:npm_config_python="C:\path\to\python.exe" # PowerShell
2014-06-05 15:18:15 -07:00
```
2020-01-28 17:14:59 -05:00
3. If the `PYTHON` environment variable is set to the path of a Python executable,
2023-11-03 07:03:57 -07:00
then that version will be used if it is a supported version.
2019-10-08 08:57:31 -07:00
2020-01-28 17:14:59 -05:00
4. If the `NODE_GYP_FORCE_PYTHON` environment variable is set to the path of a
2019-10-08 08:57:31 -07:00
Python executable, it will be used instead of any of the other configured or
2023-11-03 07:03:57 -07:00
built-in Python search paths. If it's not a compatible version, no further
2019-10-08 08:57:31 -07:00
searching will be done.
2021-11-18 20:58:02 +00:00
### Build for Third Party Node.js Runtimes
2023-11-03 07:03:57 -07:00
When building modules for third-party Node.js runtimes like Electron, which have
2021-11-18 20:58:02 +00:00
different build configurations from the official Node.js distribution, you
should use `--dist-url` or `--nodedir` flags to specify the headers of the
runtime to build for.
Also when `--dist-url` or `--nodedir` flags are passed, node-gyp will use the
`config.gypi` shipped in the headers distribution to generate build
configurations, which is different from the default mode that would use the
`process.config` object of the running Node.js instance.
Some old versions of Electron shipped malformed `config.gypi` in their headers
distributions, and you might need to pass `--force-process-config` to node-gyp
to work around configuration errors.
2019-07-24 23:00:03 -07:00
## How to Use
2012-03-14 16:39:15 -07:00
2023-11-03 07:03:57 -07:00
To compile your native addon first go to its root directory:
2012-03-14 16:39:15 -07:00
``` bash
2021-10-07 20:21:11 +00:00
cd my_node_addon
2012-03-14 16:39:15 -07:00
```
The next step is to generate the appropriate project build files for the current
platform. Use `configure` for that:
``` bash
2021-10-07 20:21:11 +00:00
node-gyp configure
2012-03-14 16:39:15 -07:00
```
2017-05-09 14:46:02 -07:00
Auto-detection fails for Visual C++ Build Tools 2015, so `--msvs_version=2015`
needs to be added (not needed when run by npm as configured above):
``` bash
2021-10-07 20:21:11 +00:00
node-gyp configure --msvs_version=2015
2017-05-09 14:46:02 -07:00
```
2019-07-24 23:00:03 -07:00
__Note__: The `configure` step looks for a `binding.gyp` file in the current
directory to process. See below for instructions on creating a `binding.gyp` file.
2012-03-14 16:39:15 -07:00
Now you will have either a `Makefile` (on Unix platforms) or a `vcxproj` file
2019-07-24 23:00:03 -07:00
(on Windows) in the `build/` directory. Next, invoke the `build` command:
2012-03-14 16:39:15 -07:00
``` bash
2021-10-07 20:21:11 +00:00
node-gyp build
2012-03-14 16:39:15 -07:00
```
Now you have your compiled `.node` bindings file! The compiled bindings end up
2019-07-24 23:00:03 -07:00
in `build/Debug/` or `build/Release/` , depending on the build mode. At this point,
you can require the `.node` file with Node.js and run your tests!
2012-03-14 16:39:15 -07:00
__Note:__ To create a _Debug_ build of the bindings file, pass the `--debug` (or
2019-07-24 23:00:03 -07:00
`-d` ) switch when running either the `configure` , `build` or `rebuild` commands.
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
## The `binding.gyp` file
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
A `binding.gyp` file describes the configuration to build your module, in a
JSON-like format. This file gets placed in the root of your package, alongside
`package.json` .
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
A barebones `gyp` file appropriate for building a Node.js addon could look like:
2012-03-14 16:39:15 -07:00
2019-10-08 08:57:31 -07:00
```python
2012-03-14 16:39:15 -07:00
{
2012-03-28 19:36:44 -07:00
"targets": [
2012-03-14 16:39:15 -07:00
{
2012-03-28 19:36:44 -07:00
"target_name": "binding",
"sources": [ "src/binding.cc" ]
2012-03-14 16:39:15 -07:00
}
]
}
```
2020-01-28 17:14:59 -05:00
## Further reading
2021-10-07 20:21:11 +00:00
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.
2020-01-28 17:14:59 -05:00
Some additional resources for Node.js native addons and writing `gyp` configuration files:
2012-03-14 16:39:15 -07:00
2015-03-20 01:40:50 -07:00
* ["Going Native" a nodeschool.io tutorial ](http://nodeschool.io/#goingnative )
2023-11-03 07:03:57 -07:00
* ["Hello World" node addon example ](https://github.com/nodejs/node/tree/main/test/addons/hello-world )
2016-01-28 18:11:35 -08:00
* [gyp user documentation ](https://gyp.gsrc.io/docs/UserDocumentation.md )
* [gyp input format reference ](https://gyp.gsrc.io/docs/InputFormatReference.md )
2021-10-07 20:21:11 +00:00
* [*"binding.gyp" files out in the wild* wiki page ](./docs/binding.gyp-files-in-the-wild.md )
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
## Commands
2012-03-14 16:39:15 -07:00
`node-gyp` responds to the following commands:
2012-07-24 12:34:13 -07:00
| **Command** | **Description**
|:--------------|:---------------------------------------------------------------
2016-09-22 07:59:37 -07:00
| `help` | Shows the help dialog
2012-07-24 12:34:13 -07:00
| `build` | Invokes `make` /`msbuild.exe` and builds the native addon
2015-03-20 01:40:50 -07:00
| `clean` | Removes the `build` directory if it exists
2012-07-24 12:34:13 -07:00
| `configure` | Generates project build files for the current platform
2015-03-20 01:40:50 -07:00
| `rebuild` | Runs `clean` , `configure` and `build` all in a row
2019-07-24 23:00:03 -07:00
| `install` | Installs Node.js header files for the given version
| `list` | Lists the currently installed Node.js header versions
| `remove` | Removes the Node.js header files for the given version
2016-09-22 07:59:37 -07:00
2019-07-24 23:00:03 -07:00
## Command Options
2016-09-22 07:59:37 -07:00
`node-gyp` accepts the following command options:
| **Command** | **Description**
|:----------------------------------|:------------------------------------------
2019-07-24 23:00:03 -07:00
| `-j n` , `--jobs n` | Run `make` in parallel. The value `max` will use all available CPU cores
| `--target=v6.2.1` | Node.js version to build for (default is `process.version` )
2016-09-22 07:59:37 -07:00
| `--silly` , `--loglevel=silly` | Log all progress to console
| `--verbose` , `--loglevel=verbose` | Log most progress to console
| `--silent` , `--loglevel=silent` | Don't log anything to console
2019-07-24 23:00:03 -07:00
| `debug` , `--debug` | Make Debug build (default is `Release` )
2016-09-22 07:59:37 -07:00
| `--release` , `--no-debug` | Make Release build
| `-C $dir` , `--directory=$dir` | Run command in different directory
2019-07-24 23:00:03 -07:00
| `--make=$make` | Override `make` command (e.g. `gmake` )
2016-09-22 07:59:37 -07:00
| `--thin=yes` | Enable thin static libraries
| `--arch=$arch` | Set target architecture (e.g. ia32)
| `--tarball=$path` | Get headers from a local tarball
2019-07-24 23:00:03 -07:00
| `--devdir=$path` | SDK download directory (default is OS cache directory)
2016-09-22 07:59:37 -07:00
| `--ensure` | Don't reinstall headers if already present
| `--dist-url=$url` | Download header tarball from custom URL
2020-03-19 12:53:27 -04:00
| `--proxy=$url` | Set HTTP(S) proxy for downloading header tarball
| `--noproxy=$urls` | Set urls to ignore proxies when downloading header tarball
2016-09-22 07:59:37 -07:00
| `--cafile=$cafile` | Override default CA chain (to download tarball)
2018-07-18 13:55:52 -07:00
| `--nodedir=$path` | Set the path to the node source code
2019-10-08 08:57:31 -07:00
| `--python=$path` | Set path to the Python binary
2019-07-24 23:00:03 -07:00
| `--msvs_version=$version` | Set Visual Studio version (Windows only)
| `--solution=$solution` | Set Visual Studio Solution version (Windows only)
2021-11-18 20:58:02 +00:00
| `--force-process-config` | Force using runtime's `process.config` object to generate `config.gypi` file
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
## Configuration
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
### Environment variables
2017-06-05 16:31:14 -07:00
2019-07-24 23:00:03 -07:00
Use the form `npm_config_OPTION_NAME` for any of the command options listed
above (dashes in option names should be replaced by underscores).
2017-06-05 16:31:14 -07:00
2019-07-24 23:00:03 -07:00
For example, to set `devdir` equal to `/tmp/.gyp` , you would:
2017-06-05 16:31:14 -07:00
2019-07-24 23:00:03 -07:00
Run this on Unix:
2017-06-05 16:31:14 -07:00
2019-07-24 23:00:03 -07:00
```bash
2021-10-07 20:21:11 +00:00
export npm_config_devdir=/tmp/.gyp
2019-07-24 23:00:03 -07:00
```
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
Or this on Windows:
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
```console
2021-10-07 20:21:11 +00:00
set npm_config_devdir=c:\temp\.gyp
2019-07-24 23:00:03 -07:00
```
2023-11-03 07:03:57 -07:00
### `npm` configuration for npm versions before v9
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
Use the form `OPTION_NAME` for any of the command options listed above.
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
For example, to set `devdir` equal to `/tmp/.gyp` , you would run:
```bash
2021-10-07 20:21:11 +00:00
npm config set [--global] devdir /tmp/.gyp
2019-07-24 23:00:03 -07:00
```
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
**Note:** Configuration set via `npm` will only be used when `node-gyp`
is run via `npm` , not when `node-gyp` is run directly.
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
## License
2012-03-14 16:39:15 -07:00
2019-07-24 23:00:03 -07:00
`node-gyp` is available under the MIT license. See the [LICENSE
file](LICENSE) for details.