or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-electron-lite

Zero-dependency package for downloading and installing Electron (v2.0.16) prebuilt binary from GitHub releases

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/electron-lite@2019.1.x

To install, run

npx @tessl/cli install tessl/npm-electron-lite@2019.1.0

index.mddocs/

Electron Lite

Electron Lite is a zero-dependency npm package that provides a lightweight solution for downloading and installing the Electron (v2.0.16) prebuilt binary from GitHub releases. It enables developers to use Electron for headless browser operations, web scraping, screenshot generation, and automated web tasks without requiring the full Electron package.

The package serves as a lightweight alternative that automatically handles platform-specific Electron binary download and installation, supporting both command-line usage and programmatic API access.

Package Information

  • Package Name: electron-lite
  • Package Type: npm
  • Language: JavaScript (Node.js)
  • Installation: npm install electron-lite
  • Supported Platforms: Darwin (macOS), Linux x64
  • Node.js Version: >=10.0
  • Electron Version: v2.0.16 (default, configurable via npm_config_electron_version)

Core Imports

const electronLite = require('electron-lite');

For accessing specific utilities through destructuring:

const { assertThrow, identity, nop, fs, path } = require('electron-lite');

For accessing built-in Node.js modules:

const { fs, path, http, crypto } = require('electron-lite');

Basic Usage

Command Line Interface

# Install globally
npm install -g electron-lite

# Run Electron with arguments
electron --help
electron --version
electron /path/to/your/app

Programmatic Usage

const electronLite = require('electron-lite');

// Use utility functions
const result = electronLite.identity("test value");
console.log(result); // "test value"

// Access Node.js built-in modules through electron-lite
const fs = electronLite.fs;
const path = electronLite.path;

// Use assertion utility
electronLite.assertThrow(true, "This will not throw");

// Access self-references
console.log(electronLite.local === electronLite); // true
console.log(electronLite.electron === electronLite); // true

// Check environment
if (!electronLite.isBrowser) {
    console.log('Running in Node.js environment');
}

Screenshot Example

/*
This example creates a screenshot of a webpage using Electron.
Save as example.js and run with:
npm install electron-lite && ./node_modules/.bin/electron . --disable-overlay-scrollbar --enable-logging
*/

const fs = require('fs');

// Initialize Electron app
let modeNext = 0;
let options;

const onNext = function (data) {
    modeNext += 1;
    switch (modeNext) {
        case 1:
            // Wait for electron to init
            try {
                require("app").on("ready", onNext);
            } catch (ignore) {
                require("electron").app.once("ready", onNext);
            }
            break;
        case 2:
            // Init options and browserWindow
            options = {frame: false, height: 768, width: 1024, x: 0, y: 0};
            try {
                options.BrowserWindow = require("browser-window");
            } catch (ignore) {
                options.BrowserWindow = require("electron").BrowserWindow;
            }
            options.browserWindow = new options.BrowserWindow(options);
            
            // Goto next step when webpage is loaded
            options.browserWindow.webContents.once("did-stop-loading", onNext);
            
            // Open URL
            (options.browserWindow.loadUrl || options.browserWindow.loadURL).call(
                options.browserWindow,
                "https://electron.atom.io"
            );
            break;
        case 3:
            // Screenshot webpage
            options.browserWindow.capturePage(options, onNext);
            break;
        case 4:
            // Save screenshot
            try {
                data = data.toPng();
            } catch (ignore) {
                data = data.toPNG();
            }
            fs.writeFileSync("/tmp/screenshot.png", data);
            console.log("Created screenshot file /tmp/screenshot.png");
            process.exit(0);
            break;
    }
};

onNext();

Usage Modes

The package behaves differently depending on how it's used:

CLI Mode

When run as the main module (e.g., via the electron command), the package:

  • Spawns an Electron child process
  • Passes through all command-line arguments to the Electron binary
  • Automatically downloads and installs Electron if not present
  • Exits when the child process exits

Programmatic Mode

When imported as a module (require('electron-lite')), the package:

  • Provides utility functions and Node.js module access
  • Does not spawn any child processes
  • Can be used in browser environments (with limited functionality)

Capabilities

Command Line Interface

The package provides a global electron command that acts as a wrapper for the Electron binary.

// CLI binary export
{
  "bin": {
    "electron": "lib.electron.js"
  }
}

CLI Features:

  • Automatically downloads and installs Electron v2.0.16 on first use
  • Supports all standard Electron command-line arguments
  • Platform detection (Darwin/Linux) with appropriate binary selection
  • Graceful fallback to system-installed Electron if version matches
  • Built-in help support with --help and -h flags

Utility Functions

Core utility functions for assertions, function handling, and object manipulation.

/**
 * Throws error if condition is falsy
 * @param {any} passed - Condition to test
 * @param {string|Error} message - Error message or error object
 * @throws {Error} If passed is falsy
 */
function assertThrow(passed, message);

/**
 * Returns function if exists, otherwise returns nop function
 * @param {function|undefined} fnc - Function to check
 * @returns {function} The function or nop
 */
function functionOrNop(fnc);

/**
 * Identity function that returns input value unchanged
 * @param {any} value - Value to return
 * @returns {any} The input value
 */
function identity(value);

/**
 * No-operation function that does nothing
 * @returns {undefined}
 */
function nop();

/**
 * Assigns properties from source to target for null/undefined/empty values
 * @param {object} target - Target object to modify
 * @param {object} source - Source object with default values
 */
function objectAssignDefault(target, source);

Environment Detection

/**
 * Boolean indicating if running in browser environment
 * @type {boolean}
 */
const isBrowser;

Built-in Node.js Module Access

When running in Node.js environment, provides direct access to all built-in modules:

// Core Node.js modules available as properties
const assert;      // Node.js assert module
const buffer;      // Node.js buffer module
const child_process; // Node.js child_process module
const cluster;     // Node.js cluster module
const crypto;      // Node.js crypto module
const dgram;       // Node.js dgram module
const dns;         // Node.js dns module
const domain;      // Node.js domain module
const events;      // Node.js events module
const fs;          // Node.js fs module
const http;        // Node.js http module
const https;       // Node.js https module
const net;         // Node.js net module
const os;          // Node.js os module
const path;        // Node.js path module
const querystring; // Node.js querystring module
const readline;    // Node.js readline module
const repl;        // Node.js repl module
const stream;      // Node.js stream module
const string_decoder; // Node.js string_decoder module
const timers;      // Node.js timers module
const tls;         // Node.js tls module
const tty;         // Node.js tty module
const url;         // Node.js url module
const util;        // Node.js util module
const vm;          // Node.js vm module
const zlib;        // Node.js zlib module

Debug Utilities

The package initializes a global debug utility function accessible as globalThis.debugInline.

/**
 * Global debug utility that prints arguments to stderr and returns first argument
 * Available as globalThis.debugInline (note: unicode escape in actual implementation)
 * @param {...*} var_args - Variable arguments to debug and print  
 * @returns {*} First argument passed
 */
globalThis.debugInline = function(/* ...var_args */);

Installation System

The package automatically handles Electron binary installation through a postinstall script.

Installation Features:

  • Downloads from predetermined GitHub release URLs
  • Platform-specific binary paths:
    • Darwin: external/Electron.app/Contents/MacOS/Electron
    • Linux: external/electron
  • Version-specific downloads (defaults to v2.0.16)
  • Caching and fallback mechanisms
  • Busybox integration for Linux unzip operations

Environment Configuration:

# Override Electron version (defaults to v2.0.16)
npm_config_electron_version=v3.0.0 npm install electron-lite

# Also available versions from releases.txt:
# v3.0.14, v3.1.0-beta.5, v4.0.1, v4.0.0, v2.0.16, etc.

Process Management

When used as CLI (main module), spawns child process for Electron execution:

/**
 * Child process instance running the Electron binary
 * Only available when lib.electron.js is executed as main module (CLI mode)
 * Not available when required as a module
 * @type {ChildProcess}
 */
local.child = child_process.spawn(/* ... */);

Types

/**
 * Main module export containing all utilities and Node.js modules
 */
interface ElectronLiteModule {
  // Utility functions
  assertThrow: (passed: any, message: string | Error) => void;
  functionOrNop: (fnc?: function) => function;
  identity: (value: any) => any;
  nop: () => undefined;
  objectAssignDefault: (target: object, source: object) => void;
  
  // Self-references
  local: ElectronLiteModule;
  electron: ElectronLiteModule;
  
  // Module metadata (Node.js only)
  __dirname?: string;
  
  // Environment detection
  isBrowser: boolean;
  
  // Node.js built-in modules (when not in browser)
  assert?: typeof import('assert');
  buffer?: typeof import('buffer');
  child_process?: typeof import('child_process');
  crypto?: typeof import('crypto');
  fs?: typeof import('fs');
  http?: typeof import('http');
  https?: typeof import('https');
  path?: typeof import('path');
  os?: typeof import('os');
  // ... all other Node.js built-in modules
  
  // Module metadata (Node.js only)
  __dirname?: string;
}

Error Handling

The package includes built-in error handling patterns:

  • Installation Errors: Graceful fallback to system Electron installation
  • Platform Errors: Automatic platform detection with appropriate error messages
  • Network Errors: Download retry and caching mechanisms
  • Process Errors: Proper child process cleanup and exit handling

Common error scenarios:

  • Unsupported platform (only Darwin/Linux supported)
  • Network connectivity issues during Electron download
  • File system permissions for binary installation
  • Version mismatch with system Electron installation

Platform Support

Supported Platforms:

  • Darwin (macOS): Uses Electron.app bundle format
  • Linux x64: Uses standard Linux binary format

Unsupported Platforms:

  • Windows (not included in release URLs)
  • ARM architectures
  • 32-bit systems

Dependencies

Runtime Dependencies: None (zero-dependency package)

Internal Tools:

  • busybox-i486: Included binary for Linux unzip operations
  • npm_scripts.sh: Installation and build script
  • releases.txt: Predefined list of Electron release URLs

Installation Process

  1. Package Installation: npm install electron-lite
  2. Postinstall Hook: Runs npm_scripts.sh automatically
  3. Binary Detection: Checks for system Electron with matching version
  4. Download: Fetches appropriate Electron binary if needed
  5. Extraction: Unzips and installs to external/ directory
  6. Symlink: Creates electron command in node_modules/.bin/

Version Information

  • Package Version: 2019.1.7
  • Electron Version: v2.0.16 (default, configurable via npm_config_electron_version)
  • Node.js Requirement: >=10.0
  • License: MIT