detect-electron
v0.1.0
Published
Tiny, SSR-safe utility to detect whether code is running inside Electron.
Maintainers
Readme
detect-electron
Tiny, SSR-safe utility to detect whether code is running inside Electron.
- Zero runtime dependencies
- Strict TypeScript with first-class types
- Safe to import in SSR (Next.js, Remix, Vite, Astro, SvelteKit)
- Tree-shakeable ESM + CJS bundles (~1.3 KB ESM)
- Detects renderer, main process, and
userAgentfallback
Installation
npm i detect-electronUsage
import { isElectron } from 'detect-electron';
if (isElectron()) {
// running inside Electron (renderer or main)
}You can also import the focused helpers when you need a specific signal:
import {
isElectronMain,
isElectronRenderer,
isElectronUserAgent,
} from 'detect-electron';
isElectronRenderer(); // true in the renderer process
isElectronMain(); // true in the main process
isElectronUserAgent(); // true when navigator.userAgent contains `Electron`API
isElectron(options?): boolean
Returns true when the current runtime is Electron. The check combines three independent signals, evaluated in order:
- Renderer process —
window.process.versions.electron - Main process —
process.versions.electron - User agent fallback —
navigator.userAgentcontainsElectron
options.environment lets you inject globals for testing or unusual runtimes.
import { isElectron } from 'detect-electron';
isElectron({
environment: {
process: { versions: { node: '20.0.0', electron: '28.0.0' } },
},
}); // → trueisElectronRenderer(env?): boolean
True when the resolved window exposes a renderer-side process.versions.electron shim.
isElectronMain(env?): boolean
True when the resolved process.versions.electron is a non-empty string.
isElectronUserAgent(env?): boolean
True when navigator.userAgent contains the substring Electron. Useful as a fallback when the renderer's process shim has been disabled (nodeIntegration: false without a preload bridge).
Types
interface IsElectronOptions {
readonly environment?: ElectronDetectionEnvironment;
}
interface ElectronDetectionEnvironment {
readonly window?: { readonly process?: unknown } | undefined;
readonly process?:
| {
readonly versions?: Readonly<Record<string, string | undefined>>;
}
| undefined;
readonly navigator?: { readonly userAgent?: string } | undefined;
}SSR safety
The module performs no global access at import time. Every helper reads globalThis lazily and gates each lookup, so it is safe to use in:
- Next.js (App Router and Pages Router)
- Remix
- SvelteKit
- Astro
- Vite SSR
- Any worker / edge runtime
Testing your own code
Because every public function accepts an optional environment argument, you can drive deterministic tests without polluting real globals:
import { isElectron } from 'detect-electron';
import { describe, expect, it } from 'vitest';
describe('my feature', () => {
it('takes the Electron path', () => {
expect(
isElectron({
environment: {
window: { process: { versions: { electron: '28.0.0' } } },
},
}),
).toBe(true);
});
it('takes the browser path', () => {
expect(isElectron({ environment: { window: {} } })).toBe(false);
});
});License
© 2026 Breno Polanski
Licensed under MIT
