|
| 1 | +--- |
| 2 | +authors: gabrielschulhof, NickNaso, jschlight, mhdawson, KevinEady, avivkeller |
| 3 | +--- |
| 4 | + |
| 5 | +# node-pre-gyp |
| 6 | + |
| 7 | +One of the limitations of native addons is that they must be compiled for each target platform and architecture. Without pre-built binaries, every user who installs your package must have a working C/C++ toolchain on their machine. |
| 8 | + |
| 9 | +[node-pre-gyp](https://github.com/mapbox/node-pre-gyp) solves this by letting you build binaries ahead of time, upload them to a remote location, and have users download the right binary at install time - falling back to compiling from source only if a matching binary is not available. |
| 10 | + |
| 11 | +> Note that Node-API support was added to node-pre-gyp in version 0.8.0. |
| 12 | +
|
| 13 | +> [prebuild](/learn/napi/build-tools/prebuild.md) is an alternative tool that addresses the same problem. |
| 14 | +
|
| 15 | +This page describes the changes required to a Node-API addon to support node-pre-gyp. |
| 16 | + |
| 17 | +## Amazon S3 |
| 18 | + |
| 19 | +By default, node-pre-gyp uploads binaries to [Amazon S3](https://aws.amazon.com/s3/). |
| 20 | + |
| 21 | +> The [node-pre-gyp-github](https://github.com/bchr02/node-pre-gyp-github) module adds support for publishing to GitHub Releases instead. |
| 22 | +
|
| 23 | +### Amazon S3 Requirements |
| 24 | + |
| 25 | +Before uploading you need: |
| 26 | + |
| 27 | +1. An Amazon Web Services account. |
| 28 | +2. An IAM user or role with permission to upload to S3. |
| 29 | +3. An [S3 bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingBucket.html) to host the binaries. |
| 30 | + |
| 31 | +### AWS Credentials |
| 32 | + |
| 33 | +Never store credentials in your repository. node-pre-gyp supports two common approaches for providing credentials during development: |
| 34 | + |
| 35 | +1. A `~/.node_pre_gyprc` file: |
| 36 | + |
| 37 | + ```json |
| 38 | + { |
| 39 | + "accessKeyId": "xxx", |
| 40 | + "secretAccessKey": "xxx" |
| 41 | + } |
| 42 | + ``` |
| 43 | + |
| 44 | +2. Environment variables: |
| 45 | + |
| 46 | + ```bash |
| 47 | + export node_pre_gyp_accessKeyId=xxx |
| 48 | + export node_pre_gyp_secretAccessKey=xxx |
| 49 | + ``` |
| 50 | + |
| 51 | +For CI environments, prefer IAM roles or short-lived credentials rather than long-lived access keys. See the [node-pre-gyp credentials documentation](https://github.com/mapbox/node-pre-gyp#3-configure-aws-credentials) for additional options. |
| 52 | + |
| 53 | +## package.json |
| 54 | + |
| 55 | +### The `dependencies` and `devDependencies` properties |
| 56 | + |
| 57 | +The package is now published under the `@mapbox` scope. Use `aws-sdk` as a dev dependency for the upload step. |
| 58 | + |
| 59 | +```json |
| 60 | +"dependencies": { |
| 61 | + "@mapbox/node-pre-gyp": "^1.0.0" |
| 62 | +}, |
| 63 | +"devDependencies": { |
| 64 | + "aws-sdk": "^2.0.0" |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +### The `scripts` property |
| 69 | + |
| 70 | +The `install` script should invoke node-pre-gyp with `--fallback-to-build` so that users who don't have a pre-built binary available can still compile locally: |
| 71 | + |
| 72 | +```json |
| 73 | +"scripts": { |
| 74 | + "install": "node-pre-gyp install --fallback-to-build" |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +### The `binary` property |
| 79 | + |
| 80 | +The `binary` property tells node-pre-gyp which Node-API versions your addon supports and where to find/upload binaries: |
| 81 | + |
| 82 | +```json |
| 83 | +"binary": { |
| 84 | + "module_name": "your_module", |
| 85 | + "module_path": "./lib/binding/napi-v{napi_build_version}", |
| 86 | + "remote_path": "./{module_name}/v{version}/{configuration}/", |
| 87 | + "package_name": "{platform}-{arch}-napi-v{napi_build_version}.tar.gz", |
| 88 | + "host": "https://your_bucket.s3.us-west-1.amazonaws.com", |
| 89 | + "napi_versions": [3] |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +Set `module_name` to a valid C identifier. The `napi_versions` array lists which Node-API versions to build for; `3` is a reasonable minimum for most addons. |
| 94 | + |
| 95 | +See the [node-pre-gyp docs](https://github.com/mapbox/node-pre-gyp#1-add-new-entries-to-your-packagejson) for a complete reference, including [Node-API considerations](https://github.com/mapbox/node-pre-gyp#n-api-considerations). |
| 96 | + |
| 97 | +## binding.gyp |
| 98 | + |
| 99 | +### New target |
| 100 | + |
| 101 | +Add a post-build target to copy the compiled binary to the path specified by `module_path`: |
| 102 | + |
| 103 | +```json |
| 104 | +{ |
| 105 | + "target_name": "action_after_build", |
| 106 | + "type": "none", |
| 107 | + "dependencies": ["<(module_name)"], |
| 108 | + "copies": [ |
| 109 | + { |
| 110 | + "files": ["<(PRODUCT_DIR)/<(module_name).node"], |
| 111 | + "destination": "<(module_path)" |
| 112 | + } |
| 113 | + ] |
| 114 | +} |
| 115 | +``` |
| 116 | + |
| 117 | +### NAPI_VERSION |
| 118 | + |
| 119 | +Include the Node-API version in the first target's `defines` so the header files configure themselves correctly: |
| 120 | + |
| 121 | +```json |
| 122 | +"defines": [ |
| 123 | + "NAPI_VERSION=<(napi_build_version)" |
| 124 | +] |
| 125 | +``` |
| 126 | + |
| 127 | +## JavaScript updates |
| 128 | + |
| 129 | +JavaScript code that loads the native binary must dynamically resolve the path to the correct `.node` file: |
| 130 | + |
| 131 | +```cjs |
| 132 | +const binary = require('@mapbox/node-pre-gyp'); |
| 133 | +const path = require('path'); |
| 134 | +const bindingPath = binary.find( |
| 135 | + path.resolve(path.join(__dirname, './package.json')) |
| 136 | +); |
| 137 | +const binding = require(bindingPath); |
| 138 | +``` |
| 139 | + |
| 140 | +## Build |
| 141 | + |
| 142 | +Once everything is in place, build from source: |
| 143 | + |
| 144 | +```bash |
| 145 | +npm install --build-from-source |
| 146 | +``` |
| 147 | + |
| 148 | +## Package and publish |
| 149 | + |
| 150 | +```bash |
| 151 | +./node_modules/.bin/node-pre-gyp package |
| 152 | +./node_modules/.bin/node-pre-gyp publish |
| 153 | +``` |
| 154 | + |
| 155 | +## CI and automated builds |
| 156 | + |
| 157 | +Use [GitHub Actions](https://docs.github.com/en/actions) to build, test, and publish binaries for multiple platforms and architectures. A typical workflow matrix covers `ubuntu-latest`, `macos-latest`, and `windows-latest`, plus any architecture variants you need (e.g. `x64`, `arm64`). See the node-pre-gyp repository for [example workflow configurations](https://github.com/mapbox/node-pre-gyp). |
0 commit comments