Detect if running in Electron environment
npx @tessl/cli install tessl/npm-is-electron@2.2.00
# is-electron
1
2
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.
3
4
## Package Information
5
6
- **Package Name**: is-electron
7
- **Package Type**: npm
8
- **Language**: JavaScript (with TypeScript definitions)
9
- **Installation**: `npm install is-electron`
10
11
## Core Imports
12
13
```javascript
14
const isElectron = require('is-electron');
15
```
16
17
For ES modules:
18
19
```javascript
20
import isElectron from 'is-electron';
21
```
22
23
For TypeScript:
24
25
```typescript
26
import isElectron from 'is-electron';
27
```
28
29
## Basic Usage
30
31
```javascript
32
import isElectron from 'is-electron';
33
34
if (isElectron()) {
35
console.log('Running in Electron');
36
// Electron-specific code here
37
} else {
38
console.log('Running in browser or Node.js');
39
// Standard web/Node.js code here
40
}
41
```
42
43
## Capabilities
44
45
### Electron Detection
46
47
Detects if the current JavaScript environment is running within Electron using multiple detection strategies for maximum reliability.
48
49
```javascript { .api }
50
/**
51
* Detects if running in Electron environment
52
* @returns {boolean} true if running in Electron, false otherwise
53
*/
54
function isElectron(): boolean;
55
```
56
57
The function uses three detection methods in sequence:
58
59
1. **Renderer Process Detection**: Checks for `window.process.type === 'renderer'`
60
2. **Main Process Detection**: Checks for presence of `process.versions.electron`
61
3. **User Agent Detection**: Checks if `navigator.userAgent` contains 'Electron' (fallback for nodeIntegration disabled scenarios)
62
63
**Usage Examples:**
64
65
```javascript
66
// Basic detection
67
if (isElectron()) {
68
// Enable Electron-specific features
69
const { ipcRenderer } = require('electron');
70
// ... Electron renderer process code
71
}
72
```
73
74
```javascript
75
// Conditional module loading
76
const fs = isElectron()
77
? require('fs') // Node.js fs in Electron
78
: null; // Not available in browser
79
```
80
81
```javascript
82
// Platform-specific UI behavior
83
const menuBar = isElectron()
84
? createElectronMenu() // Native menu
85
: createWebMenu(); // HTML/CSS menu
86
```
87
88
## Detection Strategy Details
89
90
The library implements a comprehensive detection approach that handles various Electron configurations:
91
92
- **Renderer Process**: Detects when running in Electron's renderer process (main window, webview, etc.)
93
- **Main Process**: Detects when running in Electron's main Node.js process
94
- **nodeIntegration Disabled**: Falls back to user agent string parsing when Node.js APIs are not available
95
96
This multi-layered approach ensures reliable detection even in edge cases like:
97
- Electron apps with `nodeIntegration: false`
98
- Different versions of Electron with varying APIs
99
- Embedded webviews and child processes
100
101
## Environment Compatibility
102
103
- **Electron Main Process**: ✅ Supported
104
- **Electron Renderer Process**: ✅ Supported
105
- **Node.js**: ✅ Returns `false` (not Electron)
106
- **Web Browsers**: ✅ Returns `false` (not Electron)
107
- **Electron with nodeIntegration disabled**: ✅ Supported via user agent detection
108
109
## Types
110
111
```typescript { .api }
112
declare function isElectron(): boolean;
113
export = isElectron;
114
```