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.

Using Node.js APIs i | Electron.js Desktop App Development l | BraveProgrammer
  1. Courses
  2. /
  3. Electron.js Desktop App Development

Lesson 2 of 28

·javascript

Using Node.js APIs in the Main Process

9 mins

Course progress7%

What you'll learn

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

4 points
  • The main process can require any npm package, including native addons.
  • Use async/await with the `fs.promises` API for clean file handling.
  • Child processes (`exec`, `spawn`) let you run external commands.
  • All heavy or privileged work should stay in the main process.

Using Node.js APIs in the Main Process

Because the main process runs plain Node, you can require any built‑in or third‑party module.

Example: File system access

const fs = require('fs').promises;
async function readConfig(){
  const data = await fs.readFile(path.join(app.getPath('userData'), 'config.json'), 'utf‑8');
  return JSON.parse(data);
}

Example: Child processes

const { exec } = require('child_process');
exec('git --version', (err, stdout) => {
  if (err) return console.error(err);
  console.log('Git version:', stdout.trim());
});

Example: Native modules

Install a library that needs compilation (e.g., sharp for image processing) and use it directly in the main process.

const sharp = require('sharp');
sharp('input.png').resize(200).toFile('output.png');

Remember: Never expose these Node APIs directly to the renderer – always go through a preload bridge.

Make it stickPractice what you've learned1 quiz · 5 practical tests · Optional activities from Electron.js Desktop App DevelopmentBrowse activities
Previous lessonWhat is Electron?Continue to lesson 3File System Access – Reading & WritingBack to Electron.js Desktop App Development curriculum