Cross-Platform-Apps haben fast immer Stellen, an denen sich macOS, Windows und Linux unterscheiden — Tastatur-Modifier, Quit-Verhalten, Pfade. process.platform ist die zentrale Detection-Variable. Hier die wichtigsten Patterns mit den typischen Use-Cases.
Die drei Plattform-Strings
process.platform | Plattform |
|---|---|
'darwin' | macOS |
'win32' | Windows (auch 64-bit!) |
'linux' | Linux |
'freebsd' | FreeBSD |
'aix' | AIX |
'win32' heißt nicht „nur 32-bit" — es ist der Plattform-Identifier von Node, kommt vom historischen Windows-Subsystem-Namen. Auch auf 64-bit-Windows ist es 'win32'.
Standard-Patterns
const isMac = process.platform === 'darwin';
const isWindows = process.platform === 'win32';
const isLinux = process.platform === 'linux';
// Klassiker: window-all-closed
app.on('window-all-closed', () => {
if (!isMac) app.quit();
});
// Modifier-Taste
const cmdKey = isMac ? 'Cmd' : 'Ctrl';
// Plattform-spezifischer Code-Pfad
if (isMac) {
app.dock.setBadge('5');
} else {
app.setBadgeCount(5);
}Die isMac/isWindows-Konstanten sind der idiomatische Weg — vermeidet process.platform === 'darwin'-Tippfehler und macht Code lesbarer.
Architektur-Detection
process.arch;
// 'x64' (Intel/AMD 64-bit)
// 'arm64' (Apple Silicon, ARM64-Linux)
// 'ia32' (32-bit, sehr selten)
const isAppleSilicon = isMac && process.arch === 'arm64';
const isIntelMac = isMac && process.arch === 'x64';Apple-Silicon-Detection wird relevant, wenn du z. B. native Tools mitlieferst — die müssen architektur-spezifisch sein. electron-builder baut für beide und erstellt ggf. Universal-Binaries.
Typische plattform-spezifische Stellen
| Bereich | macOS | Windows | Linux |
|---|---|---|---|
| App-Quit | bei Cmd+Q | bei letztem Fenster | bei letztem Fenster |
| Modifier-Taste | Cmd | Ctrl | Ctrl |
| Auto-Launch | app.setLoginItemSettings | dito | manuell ~/.config/autostart/ |
| Tray-Icon | Menüleiste | Notification-Area | distro-abhängig |
| Datei-Dialog | nativer macOS-Dialog | nativer Win-Dialog | GTK |
| Code-Signing | Pflicht für Distribution | empfohlen | nicht üblich |
| Auto-Update | Squirrel.Mac / electron-updater | Squirrel.Windows / electron-updater | AppImage / electron-updater |
Die meisten Plattform-Unterschiede sind im Electron-API schon abstrahiert (Tray, Menü). Die Stellen, wo du selbst verzweigen musst: Quit-Verhalten, Modifier, Auto-Launch.
Pattern: zentrale Plattform-Konstanten
Bei wachsenden Apps lohnt es sich, die Detection einmal zentral zu machen:
export const platform = process.platform;
export const arch = process.arch;
export const isMac = platform === 'darwin';
export const isWindows = platform === 'win32';
export const isLinux = platform === 'linux';
export const isAppleSilicon = isMac && arch === 'arm64';
export const isIntelMac = isMac && arch === 'x64';
export const modifierKey = isMac ? 'Cmd' : 'Ctrl';
export const altKey = isMac ? 'Option' : 'Alt';Damit haben alle Module einen einheitlichen Import: import { isMac, modifierKey } from './platform'.
Plattform-spezifische APIs
// app.dock — nur macOS
if (isMac) {
app.dock.setBadge('5');
app.dock.bounce('informational');
}
// setBadgeCount — funktioniert auf macOS und Windows, Linux teilweise
app.setBadgeCount(5);
// setUserTasks — nur Windows
if (isWindows) {
app.setUserTasks([{
program: process.execPath,
title: 'Neues Fenster',
/* ... */
}]);
}
// touchBar — nur macOS (mit Touch Bar)
if (isMac) {
const { TouchBar } = require('electron');
// ...
}Wer plattform-spezifische APIs ohne Detection ruft, crasht auf den anderen Plattformen. Pattern: vor jedem Aufruf prüfen.
FAQ
Warum heißt Windows in process.platform win32?
Historisch — als Node geschrieben wurde, war 32-bit-Windows der Default. Der Identifier ist seitdem stabil geblieben, auch wenn 64-bit Standard ist. Auch auf 64-bit-Windows: 'win32'.
Was ist der Unterschied zwischen process.platform und os.platform()?
Identische Werte. process.platform ist immer verfügbar ohne Import, os.platform() braucht import os. Für Inline-Checks meist process.platform schneller.
Wie erkenne ich macOS vs. iPad/iOS?
Electron läuft nicht auf iPad/iOS — daher: 'darwin' ist immer macOS. iPadOS und iOS sind eigene OS, die Electron nicht supported.
Funktioniert OS-Detection im Renderer?
process.platform ist im Sandbox-Renderer verfügbar — es ist eine der wenigen process-Properties, die die Sandbox durchlässt. Im klassischen Renderer (ohne Sandbox) sowieso. Daher: auch im Web-Code nutzbar, ohne IPC.
Wie unterscheide ich Linux-Distributionen?
Electron/Node bietet das nicht direkt. Workaround: /etc/os-release lesen — strukturierte Datei mit NAME, VERSION_ID etc. Aber: 90 % der Cross-Platform-Logik braucht das nicht — die Linux-Distros sind Electron-API-mäßig sehr ähnlich.
Sollte ich Browser-User-Agent auch prüfen?
Im Renderer: nein. Du läufst immer in Chromium, nicht in Safari/Firefox. Browser-Detection-Code aus Web-Apps ist in Electron meist unnötig oder sogar schädlich (falsche Annahmen über Features).