Skip to content

Build your personal knowledge base with Trilium Notes

fix(core): desktop crashing due to missing platform check

Elian Doran247108f347d8
diff --git a/CLAUDE.md b/CLAUDE.md
index a98c0da3fa..9778f0de7b 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -132,6 +132,19 @@ Fluent builder pattern: `.child()`, `.class()`, `.css()` chaining with position-
`packages/trilium-core/src/services/platform.ts` defines `PlatformProvider` interface with implementations in `apps/desktop/`, `apps/server/`, and `apps/client-standalone/`. Singleton via `initPlatform()`/`getPlatform()`.
+**PlatformProvider** provides:
+- `crash(message)` — Platform-specific fatal error handling
+- `getEnv(key)` — Environment variable access (server/desktop use `process.env`, standalone maps URL query params like `?safeMode` → `TRILIUM_SAFE_MODE`)
+- `isElectron`, `isMac`, `isWindows` — Platform detection flags
+
+**Critical rules for `trilium-core`**:
+- **No `process.env` in core** — use `getPlatform().getEnv()` instead (not available in standalone/browser)
+- **No `import path from "path"` in core** — Node's `path` module is externalized in browser builds. Use `packages/trilium-core/src/services/utils/path.ts` for `extname()`/`basename()` equivalents
+- **No Node.js built-in modules in core** — core runs in both Node.js and the browser (standalone). Use platform-agnostic alternatives or platform providers
+- **Platform detection via functions** — `isElectron()`, `isMac()`, `isWindows()` from `utils/index.ts` are functions (not constants) that call `getPlatform()`. They can only be called after `initializeCore()`, not at module top-level. If used in static definitions, wrap in a closure: `value: () => isWindows() ? "0.9" : "1.0"`
+- **Barrel import caution** — `import { x } from "@triliumnext/core"` loads ALL core exports. Early-loading modules like `config.ts` should import specific subpaths (e.g. `@triliumnext/core/src/services/utils/index`) to avoid circular dependencies or initialization ordering issues
+- **Electron IPC** — In desktop mode, client API calls use Electron IPC (not HTTP). The IPC handler in `apps/server/src/routes/electron.ts` must be registered via `utils.isElectron` from the **server's** utils (which correctly checks `process.versions["electron"]`), not from core's utils
+
### Database
SQLite via `better-sqlite3`. SQL abstraction in `packages/trilium-core/src/services/sql/` with `DatabaseProvider` interface, prepared statement caching, and transaction support.
diff --git a/apps/client-standalone/src/lightweight/platform_provider.ts b/apps/client-standalone/src/lightweight/platform_provider.ts
index 7c1d3c908c..85f3bc18bc 100644
--- a/apps/client-standalone/src/lightweight/platform_provider.ts
+++ b/apps/client-standalone/src/lightweight/platform_provider.ts
@@ -7,6 +7,9 @@ const QUERY_TO_ENV: Record<string, string> = {
};
export default class StandalonePlatformProvider implements PlatformProvider {
+ readonly isElectron = false;
+ readonly isMac = false;
+ readonly isWindows = false;
private envMap: Record<string, string> = {};
diff --git a/apps/desktop/src/platform_provider.ts b/apps/desktop/src/platform_provider.ts
index 01edf6087f..72e9d6cb38 100644
--- a/apps/desktop/src/platform_provider.ts
+++ b/apps/desktop/src/platform_provider.ts
@@ -2,6 +2,10 @@ import { PlatformProvider, t } from "@triliumnext/core";
import electron from "electron";
export default class DesktopPlatformProvider implements PlatformProvider {
+ readonly isElectron = true;
+ readonly isMac = process.platform === "darwin";
+ readonly isWindows = process.platform === "win32";
+
crash(message: string): void {
electron.dialog.showErrorBox(t("modals.error_title"), message);
electron.app.exit(1);
diff --git a/apps/server/src/platform_provider.ts b/apps/server/src/platform_provider.ts
index fe989370e9..ae3433f47c 100644
--- a/apps/server/src/platform_provider.ts
+++ b/apps/server/src/platform_provider.ts
@@ -1,6 +1,10 @@
import { getLog, PlatformProvider } from "@triliumnext/core";
export default class ServerPlatformProvider implements PlatformProvider {
+ readonly isElectron = !!process.versions["electron"];
+ readonly isMac = process.platform === "darwin";
+ readonly isWindows = process.platform === "win32";
+
crash(message: string): void {
getLog().error(message);
process.exit(1);
diff --git a/packages/trilium-core/src/services/keyboard_actions.ts b/packages/trilium-core/src/services/keyboard_actions.ts
index 704a639adb..268c0487da 100644
--- a/packages/trilium-core/src/services/keyboard_actions.ts
+++ b/packages/trilium-core/src/services/keyboard_actions.ts
@@ -20,7 +20,7 @@ function getDefaultKeyboardActions() {
friendlyName: t("keyboard_action_names.back-in-note-history"),
iconClass: "bx bxs-chevron-left",
// Mac has a different history navigation shortcuts - https://github.com/zadam/trilium/issues/376
- defaultShortcuts: isMac ? ["CommandOrControl+["] : ["Alt+Left"],
+ defaultShortcuts: isMac() ? ["CommandOrControl+["] : ["Alt+Left"],
description: t("keyboard_actions.back-in-note-history"),
scope: "window"
},
@@ -29,7 +29,7 @@ function getDefaultKeyboardActions() {
friendlyName: t("keyboard_action_names.forward-in-note-history"),
iconClass: "bx bxs-chevron-right",
// Mac has a different history navigation shortcuts - https://github.com/zadam/trilium/issues/376
- defaultShortcuts: isMac ? ["CommandOrControl+]"] : ["Alt+Right"],
+ defaultShortcuts: isMac() ? ["CommandOrControl+]"] : ["Alt+Right"],
description: t("keyboard_actions.forward-in-note-history"),
scope: "window"
},
@@ -153,7 +153,7 @@ function getDefaultKeyboardActions() {
actionName: "moveNoteUp",
friendlyName: t("keyboard_action_names.move-note-up"),
iconClass: "bx bx-up-arrow-alt",
- defaultShortcuts: isMac ? ["Alt+Up"] : ["CommandOrControl+Up"],
+ defaultShortcuts: isMac() ? ["Alt+Up"] : ["CommandOrControl+Up"],
description: t("keyboard_actions.move-note-up"),
scope: "note-tree"
},
@@ -161,7 +161,7 @@ function getDefaultKeyboardActions() {
actionName: "moveNoteDown",
friendlyName: t("keyboard_action_names.move-note-down"),
iconClass: "bx bx-down-arrow-alt",
- defaultShortcuts: isMac ? ["Alt+Down"] : ["CommandOrControl+Down"],
+ defaultShortcuts: isMac() ? ["Alt+Down"] : ["CommandOrControl+Down"],
description: t("keyboard_actions.move-note-down"),
scope: "note-tree"
},
@@ -169,7 +169,7 @@ function getDefaultKeyboardActions() {
actionName: "moveNoteUpInHierarchy",
friendlyName: t("keyboard_action_names.move-note-up-in-hierarchy"),
iconClass: "bx bx-arrow-from-bottom",
- defaultShortcuts: isMac ? ["Alt+Left"] : ["CommandOrControl+Left"],
+ defaultShortcuts: isMac() ? ["Alt+Left"] : ["CommandOrControl+Left"],
description: t("keyboard_actions.move-note-up-in-hierarchy"),
scope: "note-tree"
},
@@ -177,7 +177,7 @@ function getDefaultKeyboardActions() {
actionName: "moveNoteDownInHierarchy",
friendlyName: t("keyboard_action_names.move-note-down-in-hierarchy"),
iconClass: "bx bx-arrow-from-top",
- defaultShortcuts: isMac ? ["Alt+Right"] : ["CommandOrControl+Right"],
+ defaultShortcuts: isMac() ? ["Alt+Right"] : ["CommandOrControl+Right"],
description: t("keyboard_actions.move-note-down-in-hierarchy"),
scope: "note-tree"
},
@@ -282,7 +282,7 @@ function getDefaultKeyboardActions() {
actionName: "openNewTab",
friendlyName: t("keyboard_action_names.open-new-tab"),
iconClass: "bx bx-plus",
- defaultShortcuts: isElectron ? ["CommandOrControl+T"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+T"] : [],
description: t("keyboard_actions.open-new-tab"),
scope: "window"
},
@@ -290,7 +290,7 @@ function getDefaultKeyboardActions() {
actionName: "closeActiveTab",
friendlyName: t("keyboard_action_names.close-active-tab"),
iconClass: "bx bx-minus",
- defaultShortcuts: isElectron ? ["CommandOrControl+W"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+W"] : [],
description: t("keyboard_actions.close-active-tab"),
scope: "window"
},
@@ -298,7 +298,7 @@ function getDefaultKeyboardActions() {
actionName: "reopenLastTab",
friendlyName: t("keyboard_action_names.reopen-last-tab"),
iconClass: "bx bx-undo",
- defaultShortcuts: isElectron ? ["CommandOrControl+Shift+T"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+Shift+T"] : [],
isElectronOnly: true,
description: t("keyboard_actions.reopen-last-tab"),
scope: "window"
@@ -307,7 +307,7 @@ function getDefaultKeyboardActions() {
actionName: "activateNextTab",
friendlyName: t("keyboard_action_names.activate-next-tab"),
iconClass: "bx bx-skip-next",
- defaultShortcuts: isElectron ? ["CommandOrControl+Tab", "CommandOrControl+PageDown"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+Tab", "CommandOrControl+PageDown"] : [],
description: t("keyboard_actions.activate-next-tab"),
scope: "window"
},
@@ -315,7 +315,7 @@ function getDefaultKeyboardActions() {
actionName: "activatePreviousTab",
friendlyName: t("keyboard_action_names.activate-previous-tab"),
iconClass: "bx bx-skip-previous",
- defaultShortcuts: isElectron ? ["CommandOrControl+Shift+Tab", "CommandOrControl+PageUp"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+Shift+Tab", "CommandOrControl+PageUp"] : [],
description: t("keyboard_actions.activate-previous-tab"),
scope: "window"
},
@@ -757,7 +757,7 @@ function getDefaultKeyboardActions() {
actionName: "openDevTools",
friendlyName: t("keyboard_action_names.open-developer-tools"),
iconClass: "bx bx-bug-alt",
- defaultShortcuts: isElectron ? ["CommandOrControl+Shift+I"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+Shift+I"] : [],
isElectronOnly: true,
description: t("keyboard_actions.open-dev-tools"),
scope: "window"
@@ -766,7 +766,7 @@ function getDefaultKeyboardActions() {
actionName: "findInText",
friendlyName: t("keyboard_action_names.find-in-text"),
iconClass: "bx bx-search",
- defaultShortcuts: isElectron ? ["CommandOrControl+F"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+F"] : [],
description: t("keyboard_actions.find-in-text"),
scope: "window"
},
@@ -790,7 +790,7 @@ function getDefaultKeyboardActions() {
actionName: "zoomOut",
friendlyName: t("keyboard_action_names.zoom-out"),
iconClass: "bx bx-zoom-out",
- defaultShortcuts: isElectron ? ["CommandOrControl+-"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+-"] : [],
isElectronOnly: true,
description: t("keyboard_actions.zoom-out"),
scope: "window"
@@ -800,7 +800,7 @@ function getDefaultKeyboardActions() {
friendlyName: t("keyboard_action_names.zoom-in"),
iconClass: "bx bx-zoom-in",
description: t("keyboard_actions.zoom-in"),
- defaultShortcuts: isElectron ? ["CommandOrControl+="] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+="] : [],
isElectronOnly: true,
scope: "window"
},
@@ -809,7 +809,7 @@ function getDefaultKeyboardActions() {
friendlyName: t("keyboard_action_names.reset-zoom-level"),
iconClass: "bx bx-search-alt",
description: t("keyboard_actions.reset-zoom-level"),
- defaultShortcuts: isElectron ? ["CommandOrControl+0"] : [],
+ defaultShortcuts: isElectron() ? ["CommandOrControl+0"] : [],
isElectronOnly: true,
scope: "window"
},
@@ -834,7 +834,7 @@ function getDefaultKeyboardActions() {
/*
* Apply macOS-specific tweaks.
*/
- const platformModifier = isMac ? "Meta" : "Ctrl";
+ const platformModifier = isMac() ? "Meta" : "Ctrl";
for (const action of DEFAULT_KEYBOARD_ACTIONS) {
if ("defaultShortcuts" in action && action.defaultShortcuts) {
diff --git a/packages/trilium-core/src/services/options_init.ts b/packages/trilium-core/src/services/options_init.ts
index f1f76dd23b..730b9791a7 100644
--- a/packages/trilium-core/src/services/options_init.ts
+++ b/packages/trilium-core/src/services/options_init.ts
@@ -80,7 +80,7 @@ const defaultOptions: DefaultOption[] = [
{ name: "revisionSnapshotNumberLimit", value: "-1", isSynced: true },
{ name: "protectedSessionTimeout", value: "600", isSynced: true },
{ name: "protectedSessionTimeoutTimeScale", value: "60", isSynced: true },
- { name: "zoomFactor", value: isWindows ? "0.9" : "1.0", isSynced: false },
+ { name: "zoomFactor", value: () => isWindows() ? "0.9" : "1.0", isSynced: false },
{ name: "overrideThemeFonts", value: "false", isSynced: false },
{ name: "mainFontFamily", value: "theme", isSynced: false },
{ name: "mainFontSize", value: "100", isSynced: false },
diff --git a/packages/trilium-core/src/services/platform.ts b/packages/trilium-core/src/services/platform.ts
index 91efa22f06..3a7fcfc6b8 100644
--- a/packages/trilium-core/src/services/platform.ts
+++ b/packages/trilium-core/src/services/platform.ts
@@ -5,6 +5,9 @@ export interface PlatformProvider {
crash(message: string): void;
/** Returns the value of an environment variable, or undefined if not set. */
getEnv(key: string): string | undefined;
+ readonly isElectron: boolean;
+ readonly isMac: boolean;
+ readonly isWindows: boolean;
}
let platformProvider: PlatformProvider | null = null;
diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.ts
index cd76e0325b..f417db8aa6 100644
--- a/packages/trilium-core/src/services/utils/index.ts
+++ b/packages/trilium-core/src/services/utils/index.ts
@@ -1,4 +1,5 @@
import { getCrypto } from "../encryption/crypto";
+import { getPlatform } from "../platform";
import { sanitizeFileName } from "../sanitizer";
import { encodeBase64 } from "./binary";
import { extensions as mimeToExt, types as extToMime } from "mime-types";
@@ -7,10 +8,9 @@ import unescape from "unescape";
import { basename, extname } from "./path";
import { NoteMeta } from "../../meta";
-// TODO: Implement platform detection.
-export const isElectron = false;
-export const isMac = false;
-export const isWindows = false;
+export function isElectron() { return getPlatform().isElectron; }
+export function isMac() { return getPlatform().isMac; }
+export function isWindows() { return getPlatform().isWindows; }
// render and book are string note in the sense that they are expected to contain empty string
const STRING_NOTE_TYPES = new Set(["text", "code", "relationMap", "search", "render", "book", "mermaid", "canvas", "webView"]);