Node.js native addon build tool
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
The core build system provides the main entry points and orchestration for node-gyp operations, handling command parsing, execution, and cross-platform build coordination.
Creates a new Gyp instance for build operations.
/**
* Creates a new Gyp instance
* @returns {Gyp} New Gyp instance
*/
function gyp(): Gyp;Main orchestrator class that extends EventEmitter for build operations and command coordination.
/**
* Main node-gyp class that handles command parsing and execution
* @extends EventEmitter
*/
class Gyp extends EventEmitter {
/**
* Package.json contents
* @type {object}
*/
package: object;
/**
* Version from package.json
* @type {string}
*/
version: string;
/**
* Development files directory path
* @type {string}
*/
devDir: string;
/**
* Map of command names to handler functions
* @type {object}
*/
commands: { [key: string]: (argv: string[]) => Promise<any> };
/**
* Configuration definitions for nopt
* @type {object}
*/
configDefs: object;
/**
* Shorthand mappings for command-line options
* @type {object}
*/
shorthands: object;
/**
* Command aliases (ls -> list, rm -> remove)
* @type {object}
*/
aliases: object;
/**
* Parsed command-line options
* @type {GypOptions}
*/
opts: GypOptions;
/**
* Remaining command-line arguments
* @type {string[]}
*/
argv: string[];
/**
* Queue of commands to execute
* @type {CommandObject[]}
*/
todo: CommandObject[];
}Parses command-line arguments and configures the gyp instance.
/**
* Parses command-line arguments and sets opts, argv, and todo properties
* @param {string[]} argv - Command-line arguments to parse
*/
parseArgv(argv: string[]): void;Usage Example:
const gyp = require('node-gyp');
const gypInstance = gyp();
// Parse arguments for debug build
gypInstance.parseArgv(['build', '--debug', '--arch=x64']);
console.log(gypInstance.opts.debug); // true
console.log(gypInstance.opts.arch); // 'x64'
console.log(gypInstance.todo); // [{ name: 'build', args: [] }]Spawns child processes for build operations with proper logging and event emission.
/**
* Spawns a child process and emits a 'spawn' event
* @param {string} command - Command to execute
* @param {string[]} args - Arguments for the command
* @param {object} [opts] - Options for child_process.spawn
* @returns {ChildProcess} The spawned child process
*/
spawn(command: string, args: string[], opts?: object): ChildProcess;Usage Example:
const gyp = require('node-gyp');
const gypInstance = gyp();
// Spawn make command
const makeProcess = gypInstance.spawn('make', ['-j4'], {
cwd: './build',
stdio: 'inherit'
});
makeProcess.on('exit', (code) => {
console.log(`Make process exited with code ${code}`);
});Returns formatted usage instructions for the command-line interface.
/**
* Returns usage instructions for node-gyp
* @returns {string} Formatted usage instructions
*/
usage(): string;Usage Example:
const gyp = require('node-gyp');
const gypInstance = gyp();
console.log(gypInstance.usage());
// Outputs:
// Usage: node-gyp <command> [options]
//
// where <command> is one of:
// - build - Invokes `msbuild` (on Windows) or `make` (on other platforms) and builds the module
// - clean - Removes any generated build files and the "out" dir
// ...Node-gyp supports extensive configuration through command-line options and environment variables.
interface GypOptions {
help?: boolean; // Show help information
arch?: string; // Target architecture (x86, x64, arm64)
cafile?: string; // CA certificate file path
debug?: boolean; // Build in debug mode
directory?: string; // Working directory
make?: string; // Make command to use
'msvs-version'?: string; // Visual Studio version (Windows)
ensure?: boolean; // Only install if not already present
solution?: string; // Solution file to build (Windows)
proxy?: string; // HTTP proxy settings
noproxy?: string; // Proxy bypass settings
devdir?: string; // Development files directory
nodedir?: string; // Node.js directory path
loglevel?: string; // Logging level (silly, verbose, info, warn, error)
python?: string; // Python executable path
'dist-url'?: string; // Distribution URL for downloads
tarball?: string; // Local tarball path
jobs?: string; // Build job parallelization
thin?: string; // Thin archive support
'force-process-config'?: boolean; // Force process configuration
}Node-gyp automatically reads configuration from environment variables:
npm_config_* - Standard npm configuration variablesnpm_package_config_node_gyp_* - Package-specific node-gyp configurationPYTHON - Python executable pathVCINSTALLDIR - Visual Studio install directory (Windows)NODE_GYP_FORCE_PYTHON - Force specific Python versionUsage Example:
# Set options via environment variables
export npm_config_python=/usr/bin/python3.8
export npm_config_msvs_version=2019
export npm_config_debug=true
# These will be automatically picked up by parseArgvinterface CommandObject {
name: string; // Command name (build, clean, configure, etc.)
args: string[]; // Arguments specific to this command
}Node-gyp provides these core commands:
build - Compile the native moduleclean - Remove build artifactsconfigure - Generate build filesrebuild - Clean, configure, and build in sequenceinstall - Install Node.js development fileslist - List installed development filesremove - Remove development filesls → listrm → removeThe Gyp class extends EventEmitter and emits events during operation:
const gyp = require('node-gyp');
const gypInstance = gyp();
// Listen for spawn events
gypInstance.on('spawn', (command, args) => {
console.log(`Spawning: ${command} ${args.join(' ')}`);
});
// Execute commands
gypInstance.parseArgv(['build', '--debug']);