A command line tool for easily making prebuilt binaries for multiple versions of Node.js, Electron or node-webkit on a specific platform
npx @tessl/cli install tessl/npm-prebuild@13.0.0Prebuild is a command line tool and JavaScript library for easily making prebuilt binaries for multiple versions of Node.js, Node-API, Electron, and NW.js on a specific platform. It automates the complex process of cross-compilation by downloading appropriate headers, managing build environments, and uploading prebuilt binaries to GitHub releases for distribution.
npm install -g prebuild (CLI) or npm install prebuild (programmatic)const { build } = require('prebuild');For individual modules:
const build = require('prebuild/build');
const upload = require('prebuild/upload');
const util = require('prebuild/util');
const prebuild = require('prebuild/prebuild');# Build for all supported ABI versions
prebuild --all
# Build for specific targets
prebuild -t 16.14.0 -t 18.0.0
# Build for Node-API
prebuild -t 3 -r napi
# Build for Electron
prebuild -t 22.0.0 -r electron
# Build and upload to GitHub
prebuild --all --upload <github-token>
# Strip debug symbols
prebuild --all --stripconst { build } = require('prebuild');
// Build for current Node.js version
build({
pkg: require('./package.json'),
log: console
}, '16.14.0', (err) => {
if (err) throw err;
console.log('Build completed');
});Prebuild is built around several key components:
bin.js) that orchestrates the build processbuild.js, prebuild.js) that manages compilationnode-gyp, cmake-js, nw-gyp, node-ninja)pack.js), stripping (strip.js), and upload (upload.js) functionalityrc.js) with command-line and config file supportCore build functionality for compiling native modules against different Node.js runtimes and versions.
/**
* Build native module for specified target and runtime
* @param opts - Build options including pkg, log, backend, debug flags
* @param version - Target version to build for
* @param cb - Callback function (err) => void
*/
function build(opts, version, cb);
interface BuildOptions {
pkg: PackageJson;
log?: Logger;
preinstall?: string;
gyp?: object;
backend?: 'node-gyp' | 'node-ninja' | 'nw-gyp' | 'cmake-js';
args?: string[];
debug?: boolean;
}Runtime configuration system supporting command-line arguments, config files, and environment variables.
/**
* Parsed configuration object with defaults and CLI arguments
*/
interface ConfigurationOptions {
target: string | string[];
runtime: 'node' | 'napi' | 'electron' | 'node-webkit';
arch: string;
platform: string;
backend: string;
debug: boolean;
strip: boolean;
upload?: string;
all: boolean;
force: boolean;
verbose: boolean;
'include-regex': RegExp;
'tag-prefix': string;
prerelease: boolean;
}GitHub releases integration for uploading prebuilt binaries with automatic release creation.
/**
* Upload prebuilt binaries to GitHub releases
* @param opts - Upload options including files, token, and release settings
* @param cb - Callback function (err, result) => void
*/
function upload(opts, cb);
interface UploadOptions {
pkg: PackageJson;
files: string[];
upload: string; // GitHub token
'tag-prefix'?: string;
prerelease?: boolean;
gh?: object;
}
interface UploadResult {
new: string[];
old: string[];
}Common utility functions for file operations, process spawning, and path generation.
/**
* Generate tar file path for prebuilt binary
* @param opts - Options containing package info and build settings
* @param abi - ABI version string
* @returns Generated tar file path
*/
function getTarPath(opts, abi): string;
/**
* Spawn child process with error handling
* @param cmd - Command to execute
* @param args - Command arguments
* @param cb - Callback function (err) => void
*/
function spawn(cmd, args, cb): void;
/**
* Execute shell command or JavaScript file
* @param item - Shell command or .js file path
* @param cb - Callback function (err) => void
*/
function run(item, cb): void;interface PackageJson {
name: string;
version: string;
binary?: {
module_name?: string;
module_path?: string;
napi_versions?: number[];
};
}
interface Logger {
info: (...args: any[]) => void;
verbose: (...args: any[]) => void;
error: (...args: any[]) => void;
}
type Runtime = 'node' | 'napi' | 'electron' | 'node-webkit';
type Backend = 'node-gyp' | 'node-ninja' | 'nw-gyp' | 'cmake-js';