or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-is-electron

Detect if running in Electron environment

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

To install, run

npx @tessl/cli install tessl/npm-is-electron@2.2.0

index.mddocs/

is-electron

is-electron is a lightweight utility library for detecting whether JavaScript code is running within an Electron application environment. It provides reliable detection across different Electron process types and configurations using multiple detection strategies.

Package Information

  • Package Name: is-electron
  • Package Type: npm
  • Language: JavaScript (with TypeScript definitions)
  • Installation: npm install is-electron

Core Imports

const isElectron = require('is-electron');

For ES modules:

import isElectron from 'is-electron';

For TypeScript:

import isElectron from 'is-electron';

Basic Usage

import isElectron from 'is-electron';

if (isElectron()) {
  console.log('Running in Electron');
  // Electron-specific code here
} else {
  console.log('Running in browser or Node.js');
  // Standard web/Node.js code here
}

Capabilities

Electron Detection

Detects if the current JavaScript environment is running within Electron using multiple detection strategies for maximum reliability.

/**
 * Detects if running in Electron environment
 * @returns {boolean} true if running in Electron, false otherwise
 */
function isElectron(): boolean;

The function uses three detection methods in sequence:

  1. Renderer Process Detection: Checks for window.process.type === 'renderer'
  2. Main Process Detection: Checks for presence of process.versions.electron
  3. User Agent Detection: Checks if navigator.userAgent contains 'Electron' (fallback for nodeIntegration disabled scenarios)

Usage Examples:

// Basic detection
if (isElectron()) {
  // Enable Electron-specific features
  const { ipcRenderer } = require('electron');
  // ... Electron renderer process code
}
// Conditional module loading
const fs = isElectron() 
  ? require('fs')           // Node.js fs in Electron
  : null;                   // Not available in browser
// Platform-specific UI behavior
const menuBar = isElectron() 
  ? createElectronMenu()    // Native menu
  : createWebMenu();        // HTML/CSS menu

Detection Strategy Details

The library implements a comprehensive detection approach that handles various Electron configurations:

  • Renderer Process: Detects when running in Electron's renderer process (main window, webview, etc.)
  • Main Process: Detects when running in Electron's main Node.js process
  • nodeIntegration Disabled: Falls back to user agent string parsing when Node.js APIs are not available

This multi-layered approach ensures reliable detection even in edge cases like:

  • Electron apps with nodeIntegration: false
  • Different versions of Electron with varying APIs
  • Embedded webviews and child processes

Environment Compatibility

  • Electron Main Process: ✅ Supported
  • Electron Renderer Process: ✅ Supported
  • Node.js: ✅ Returns false (not Electron)
  • Web Browsers: ✅ Returns false (not Electron)
  • Electron with nodeIntegration disabled: ✅ Supported via user agent detection

Types

declare function isElectron(): boolean;
export = isElectron;