Lesson 2 of 28
javascriptUsing 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.
Lesson 2 of 28
javascript9 mins
Keep these ideas in mind as you work through the lesson.
Because the main process runs plain Node, you can require any built‑in or third‑party module.
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);
}
const { exec } = require('child_process');
exec('git --version', (err, stdout) => {
if (err) return console.error(err);
console.log('Git version:', stdout.trim());
});
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.