Local data and native APIs
Choose the data location
Store application data under Electron’s per-user userData directory instead of beside source files or the executable.
8 minute lesson
A packaged application can live in a read-only folder. Its current working directory and source layout can also differ between development and production.
Electron provides platform-specific locations through app.getPath(). Use userData for small configuration and application data.
Create a dedicated subdirectory inside it:
const path = require('node:path')
function notesPath() {
return path.join(
app.getPath('userData'),
'desktop-notes-data',
'notes.json'
)
}
The actual path differs on macOS, Windows, and Linux. Let Electron choose it instead of assembling a home-directory path yourself.
Call app.getPath() only after Electron is ready. Application identity affects the default directory, so choose a stable productName before shipping.
Do not put Chromium caches beside durable notes. Electron exposes sessionData for cookies, local storage, network state, and disk cache. Those files can grow large and have a different cleanup policy.
Log the chosen path during development:
console.log(notesPath())
Open that folder, create a note, and confirm only your data file appears in the dedicated subdirectory. Remove the log before release if paths are considered sensitive in your support model.
Decide what uninstall means. Operating systems often leave userData behind, which preserves notes across reinstalls but may surprise users who expected removal.
Lesson completed