Detect if running in Electron environment
npx @tessl/cli install tessl/npm-is-electron@2.2.0is-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.
npm install is-electronconst isElectron = require('is-electron');For ES modules:
import isElectron from 'is-electron';For TypeScript:
import isElectron from 'is-electron';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
}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:
window.process.type === 'renderer'process.versions.electronnavigator.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 menuThe library implements a comprehensive detection approach that handles various Electron configurations:
This multi-layered approach ensures reliable detection even in edge cases like:
nodeIntegration: falsefalse (not Electron)false (not Electron)declare function isElectron(): boolean;
export = isElectron;