or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-electron-debug

Adds useful debug features to your Electron app

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

To install, run

npx @tessl/cli install tessl/npm-electron-debug@4.1.0

index.mddocs/

Electron Debug

Electron Debug adds useful debug features to your Electron app by providing automatic keyboard shortcuts and programmatic APIs for common development tasks. It offers DevTools integration with customizable dock states, cross-platform keyboard shortcuts, and window management utilities specifically designed for Electron application development.

Package Information

  • Package Name: electron-debug
  • Package Type: npm
  • Language: JavaScript with TypeScript definitions
  • Installation: npm install electron-debug
  • Requirements: Electron 30 or later, Node.js 18+

Core Imports

import debug from "electron-debug";

For named exports:

import { devTools, openDevTools, refresh } from "electron-debug";

CommonJS:

const debug = require("electron-debug");
const { devTools, openDevTools, refresh } = require("electron-debug");

Basic Usage

import { app, BrowserWindow } from "electron";
import debug from "electron-debug";

// Enable debug features (automatic keyboard shortcuts + DevTools)
debug();

let mainWindow;
(async () => {
  await app.whenReady();
  mainWindow = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false,
    },
  });
  // DevTools will automatically open if showDevTools: true (default)
  // Keyboard shortcuts are automatically registered
})();

Architecture

Electron Debug operates through several key mechanisms:

  • Automatic Setup: Integrates with Electron's browser-window-created event to set up debugging for each window
  • Keyboard Shortcuts: Uses electron-localshortcut to register window-specific or global shortcuts
  • Development Detection: Automatically activates only in development mode using electron-is-dev
  • Per-Window Configuration: Maintains separate DevTools options for each BrowserWindow instance
  • Cross-Platform Support: Handles platform-specific keyboard shortcuts (macOS vs Windows/Linux)

Capabilities

Main Debug Function

Initialize debug features with automatic keyboard shortcuts and optional DevTools activation.

/**
 * Install keyboard shortcuts and optionally activate DevTools on each created BrowserWindow
 * @param options - Configuration options for debug behavior
 */
function debug(options?: Options): void;

interface Options {
  /** Enable/disable debug features. Default: development mode detection */
  isEnabled?: boolean;
  /** Show DevTools on each created BrowserWindow. Default: true */
  showDevTools?: boolean;
  /** The dock state to open DevTools in. Default: 'previous' */
  devToolsMode?: 'undocked' | 'left' | 'right' | 'bottom' | 'previous' | 'detach';
  /** 
   * Customize options for each window. Allows per-window configuration.
   * @param window - The BrowserWindow instance to configure
   * @returns true (use global options), false (disable debug), or Partial<Options> (override specific options)
   */
  windowSelector?: (window: Readonly<BrowserWindow>) => boolean | Partial<Options>;
}

Usage Examples:

import debug from "electron-debug";

// Basic setup - enables all features in development
debug();

// Custom configuration
debug({
  isEnabled: true,  // Force enable even in production
  showDevTools: false,  // Don't auto-open DevTools
  devToolsMode: 'bottom',  // Dock DevTools at bottom
});

// Per-window configuration
debug({
  windowSelector: (window) => {
    // Disable debug for windows with specific titles
    if (window.getTitle() === 'Settings') {
      return false;  // Same as { isEnabled: false }
    }
    // Custom options for main window
    if (window.getTitle() === 'Main') {
      return { devToolsMode: 'right', showDevTools: true };
    }
    // Use different dock mode for secondary windows
    if (window.getTitle().startsWith('Secondary')) {
      return { devToolsMode: 'bottom', showDevTools: false };
    }
    return true;  // Use global options for other windows
  },
});

DevTools Toggle

Toggle DevTools for a specific window or the currently focused one.

/**
 * Toggle DevTools for the specified BrowserWindow instance or the focused one
 * @param window - BrowserWindow to toggle DevTools for. Default: BrowserWindow.getFocusedWindow()
 */
function devTools(window?: BrowserWindow): void;

Usage Examples:

import { devTools } from "electron-debug";
import { BrowserWindow } from "electron";

// Toggle DevTools for focused window
devTools();

// Toggle DevTools for specific window
const myWindow = new BrowserWindow();
devTools(myWindow);

DevTools Open

Open DevTools for a specific window or the currently focused one.

/**
 * Open DevTools for the specified BrowserWindow instance or the focused one
 * @param window - BrowserWindow to open DevTools for. Default: BrowserWindow.getFocusedWindow()
 */
function openDevTools(window?: BrowserWindow): void;

Usage Examples:

import { openDevTools } from "electron-debug";

// Open DevTools for focused window
openDevTools();

// Open DevTools for specific window with custom options
const debugWindow = new BrowserWindow();
openDevTools(debugWindow);

Window Refresh

Reload a BrowserWindow with cache bypass.

/**
 * Reload the specified BrowserWindow instance or the focused one
 * @param window - BrowserWindow to reload. Default: BrowserWindow.getFocusedWindow()
 */
function refresh(window?: BrowserWindow): void;

Usage Examples:

import { refresh } from "electron-debug";

// Refresh focused window
refresh();

// Refresh specific window
const targetWindow = BrowserWindow.getAllWindows()[0];
refresh(targetWindow);

Keyboard Shortcuts

When debug mode is enabled, the following keyboard shortcuts are automatically registered:

DevTools Toggle

  • macOS: <kbd>Cmd</kbd> + <kbd>Alt</kbd> + <kbd>I</kbd> or <kbd>F12</kbd>
  • Windows/Linux: <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>I</kbd> or <kbd>F12</kbd>

Window Reload

  • All Platforms: <kbd>F5</kbd>
  • macOS: <kbd>Cmd</kbd> + <kbd>R</kbd>
  • Windows/Linux: <kbd>Ctrl</kbd> + <kbd>R</kbd>

Element Inspector

  • macOS: <kbd>Cmd</kbd> + <kbd>Shift</kbd> + <kbd>C</kbd>
  • Windows/Linux: <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>C</kbd>

Opens DevTools (if not already open) and automatically activates the element inspector tool, allowing you to click on page elements to inspect them directly.

Types

interface Options {
  /** Enable/disable debug features. Default: development mode detection via electron-is-dev */
  isEnabled?: boolean;
  /** Show DevTools on each created BrowserWindow. Default: true */
  showDevTools?: boolean;
  /** 
   * The dock state to open DevTools in. Default: 'previous'
   * - 'undocked': DevTools in separate floating window
   * - 'left': Docked to left side of window (vertical split)
   * - 'right': Docked to right side of window (vertical split)
   * - 'bottom': Docked to bottom of window (horizontal split)
   * - 'previous': Use the previously used dock state
   * - 'detach': Detached in separate window (same as 'undocked')
   */
  devToolsMode?: 'undocked' | 'left' | 'right' | 'bottom' | 'previous' | 'detach';
  /**
   * Specify customized options for each window. Allows fine-grained control over debug behavior per window.
   * Called for each BrowserWindow when it's created. Enables different debug configurations for different windows.
   * @param window - The BrowserWindow instance to configure
   * @returns true (use global options), false (disable debug entirely), or Partial<Options> (override specific options)
   * @example
   * windowSelector: (window) => {
   *   if (window.getTitle() === 'Settings') return false;
   *   if (window.getTitle() === 'Main') return { devToolsMode: 'right' };
   *   return true;
   * }
   */
  windowSelector?: (window: Readonly<BrowserWindow>) => boolean | Partial<Options>;
}

Implementation Details

Keyboard Shortcut Registration

The library uses electron-localshortcut to register shortcuts either globally or per-window:

  • Global Registration: When no windowSelector is provided, shortcuts are registered globally
  • Per-Window Registration: When windowSelector is used, shortcuts are registered individually for each window based on their configuration
  • Automatic Cleanup: Shortcuts are automatically cleaned up when windows are destroyed

DevTools Management

Each window maintains its own DevTools configuration:

  • Options Storage: DevTools options are stored in a Map with the window as the key
  • State Checking: Before opening/closing DevTools, the library checks the current state to avoid conflicts
  • Mode Configuration: The devToolsMode setting is applied when DevTools are opened

Element Inspector Behavior

The element inspector keyboard shortcut (<kbd>Ctrl/Cmd</kbd> + <kbd>Shift</kbd> + <kbd>C</kbd>) works as follows:

  1. If DevTools are already open, immediately activates element inspector mode
  2. If DevTools are closed, opens them first, then activates element inspector mode once opened
  3. Uses DevToolsAPI.enterInspectElementMode() to enable the inspection tool

Error Handling

The library handles common error conditions gracefully:

  • No Focused Window: Functions safely handle cases where BrowserWindow.getFocusedWindow() returns null by doing nothing
  • DevTools State: Checks DevTools open/closed state before performing toggle operations to prevent errors
  • Window Disposal: Automatically cleans up shortcuts and options when windows are destroyed
  • Platform Compatibility: Automatically detects macOS vs Windows/Linux for appropriate keyboard shortcuts
  • Development Mode: Automatically activates only in development unless explicitly overridden via isEnabled
  • Missing Dependencies: Gracefully handles cases where electron-localshortcut or electron-is-dev are unavailable

Development vs Production

By default, electron-debug only activates in development mode (detected via the electron-is-dev package). This ensures zero runtime overhead in production builds. To override this behavior, set isEnabled: true in the options.

// Only active in development (default behavior)
debug();

// Force enable in production
debug({ isEnabled: true });

// Explicitly disable
debug({ isEnabled: false });