CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-node-gyp

Node.js native addon build tool

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

core-build-system.mddocs/

Core Build System

The core build system provides the main entry points and orchestration for node-gyp operations, handling command parsing, execution, and cross-platform build coordination.

Capabilities

Main Factory Function

Creates a new Gyp instance for build operations.

/**
 * Creates a new Gyp instance
 * @returns {Gyp} New Gyp instance
 */
function gyp(): Gyp;

Gyp Class

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[];
}

Command Line Parsing

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: [] }]

Process Spawning

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}`);
});

Usage Information

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
//     ...

Configuration System

Configuration Definitions

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
}

Environment Variable Support

Node-gyp automatically reads configuration from environment variables:

  • npm_config_* - Standard npm configuration variables
  • npm_package_config_node_gyp_* - Package-specific node-gyp configuration
  • PYTHON - Python executable path
  • VCINSTALLDIR - Visual Studio install directory (Windows)
  • NODE_GYP_FORCE_PYTHON - Force specific Python version

Usage 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 parseArgv

Command Structure

Command Object Interface

interface CommandObject {
  name: string;    // Command name (build, clean, configure, etc.)
  args: string[];  // Arguments specific to this command
}

Available Commands

Node-gyp provides these core commands:

  • build - Compile the native module
  • clean - Remove build artifacts
  • configure - Generate build files
  • rebuild - Clean, configure, and build in sequence
  • install - Install Node.js development files
  • list - List installed development files
  • remove - Remove development files

Command Aliases

  • lslist
  • rmremove

Event System

The 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']);

docs

build-commands.md

core-build-system.md

dev-file-management.md

index.md

platform-toolchain.md

utilities.md

tile.json