Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Sample electron app demonstrating how to save user data to a file.
const { app, BrowserWindow } = require('electron');
const path = require('path');
const Store = require('./store.js');
let mainWindow; //do this so that the window object doesn't get GC'd
// First instantiate the class
const store = new Store({
// We'll call our data file 'user-preferences'
configName: 'user-preferences',
defaults: {
// 800x600 is the default size of our window
windowBounds: { width: 800, height: 600 }
// When our app is ready, we'll create our BrowserWindow
app.on('ready', function() {
// First we'll get our height and width. This will be the defaults if there wasn't anything saved
let { width, height } = store.get('windowBounds');
// Pass those values in to the BrowserWindow options
mainWindow = new BrowserWindow({ width, height });
// The BrowserWindow class extends the node.js core EventEmitter class, so we use that API
// to listen to events on the BrowserWindow. The resize event is emitted when the window size changes.
mainWindow.on('resize', () => {
// The event doesn't pass us the window size, so we call the `getBounds` method which returns an object with
// the height, width, and x and y coordinates.
let { width, height } = mainWindow.getBounds();
// Now that we have them, save them using the `set` method.
store.set('windowBounds', { width, height });
mainWindow.loadURL('file://' + path.join(__dirname, 'index.html'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.