Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

File System Access – | Electron.js Desktop App Development l | BraveProgrammer
  1. Courses
  2. /
  3. Electron.js Desktop App Development

Lesson 3 of 28

·javascript

File System Access – Reading & Writing

10 mins

Course progress11%

What you'll learn

Keep these ideas in mind as you work through the lesson.

4 points
  • `app.getPath('userData')` gives a safe folder for persistent app data.
  • Use the **Promise‑based `fs` API** for async reads/writes.
  • Always handle errors – the file may not exist on first run.
  • Show a native **dialog** (`dialog.showMessageBox`) before destructive actions.

File System Access – Reading & Writing

Electron provides app.getPath(name) to locate OS‑specific folders (userData, desktop, documents).

const { app } = require('electron');
const dataPath = app.getPath('userData');

Read a file (async):

const fs = require('fs').promises;
async function loadNotes(){
  const file = path.join(dataPath, 'notes.json');
  try {
    const raw = await fs.readFile(file, 'utf-8');
    return JSON.parse(raw);
  } catch(e){
    return [];
  }
}

Write a file:

async function saveNotes(notes){
  const file = path.join(dataPath, 'notes.json');
  await fs.writeFile(file, JSON.stringify(notes, null, 2), 'utf-8');
}

Show a dialog before overwriting:

const { dialog } = require('electron');
const { response } = await dialog.showMessageBox({
  type: 'question',
  buttons: ['Cancel', 'Overwrite'],
  defaultId: 1,
  title: 'Confirm',
  message: 'Overwrite existing file?'
});
if (response === 1) await saveNotes(notes);
Make it stickPractice what you've learned1 quiz · 5 practical tests · Optional activities from Electron.js Desktop App DevelopmentBrowse activities
Previous lessonUsing Node.js APIs in the Main ProcessContinue to lesson 4Preload Scripts & Secure Context BridgeBack to Electron.js Desktop App Development curriculum