Useful utilities for developing Electron apps and modules
You can use this module directly in both the main and renderer process.
$ npm install electron-util
Requires Electron 2.0.0 or later.
const {appReady, is} = require('electron-util');
(async () => {
await appReady;
console.log(is.macos && is.main);
//=> true
})();
Type: Object
Access the Electron APIs in both the main and renderer process without having to care about which one you're in.
For example, in the renderer process:
api.app.quit();
The app
API is usually only available in the main process.
Type: Object
Check for various things:
macos
- Running on macOSlinux
- Running on Linuxwindows
- Running on Windowsmain
- Running on the main processrenderer
- Running on the renderer processdevelopment
- Running in development, not in productionusingAsar
- The app is using ASARmacAppStore
- The app is an Mac App Store buildwindowsStore
- The app is a Windows Store AppX build
Type: Promise
Resolves when the app is ready.
Type: string
Example: 1.7.9
Electron version.
Type: string
Example: 62.0.3202
Chrome version in Electron.
Type: Function
Accepts an object with the keys as either macos
, windows
, linux
, or default
, and picks the appropriate key depending on the current platform. If no platform key is matched, the default
key is used if it exists. If the value is a function, it will be executed, and the returned value will be used.
init({
enableUnicorn: util.platform({
macos: true,
windows: false,
linux: () => false
})
});
Type: Function
Returns the active window.
Load a file into the given window using a file path relative to the root of the app.
loadFile(win, 'index.html');
You use this instead of the verbose win.loadURL(`file://…`);
Type: Function
Run some JavaScript in the active or given window.
Returns a promise for the result of the executed code or a rejected promise if the result is a rejected promise.
Type: Function
ASAR is great, but it has limitations when it comes to executing binaries. For example, child_process.spawn()
is not automatically handled. So you would have to unpack the binary, for example, with the asarUnpack
option in electron-builder
. This creates a problem as the path to the binary changes, but your path.join(__dirname, 'binary')
is not changed. To make it work you need to fix the path. That's the purpose of this method.
Before:
/Users/sindresorhus/Kap.app/Contents/Resources/app.asar/node_modules/foo/binary
After:
/Users/sindresorhus/Kap.app/Contents/Resources/app.asar.unpack/node_modules/foo/binary
Type: Function
On macOS, for security reasons, if an app is launched outside the Applications folder, it will run in a read-only disk image, which could cause subtle problems for your app. Use this method to ensure the app lives in the Applications folder.
It must not be used until the app.on('ready')
event has been emitted.
It will be a noop during development and on other systems than macOS.
It will offer to automatically move the app for the user:
Returns the height of the menu bar on macOS, or 0
if not macOS.
Get the bounds of a window as if it was centered on the screen.
Type: Object
Type: BrowserWindow
Default: Current window
The window to get the bounds of.
Type: Object
Default: Size of window
Set a new window size. Example: {width: 600, height: 400}
Set the bounds of a window. This is similar to the BrowserWindow#setBounds()
method, but it allows setting any of the x
, y
, width
, height
properties, instead of forcing you to set them all at once. The properties that are not set will just fall back to the current ones.
Type: Object
Type: BrowserWindow
Default: Current window
The window to set the bounds of.
Type: boolean
Default: false
Animate the change.
Center a window on the screen.
Type: Object
Type: BrowserWindow
Default: Current window
The window to center.
Type: Object
Default: Size of window
Set a new window size. Example: {width: 600, height: 400}
Type: boolean
Default: false
Animate the change.
Disable zooming, usually caused by pinching the trackpad on macOS or Ctrl + on Windows.
Type: BrowserWindow
Default: Current window
Type: number
A timestamp (Date.now()
) of when your app launched.
Returns a boolean
of whether it's the first time your app is launched.
It works by writing a file to app.getPath('userData')
if it doesn't exist and checks that. That means it will return true the first time you add this check to your app.
This is for non-Electron code that might be included in an Electron app. For example, if you want to add special support for Electron in a vanilla Node.js module.
const electronUtil = require('electron-util/node');
if (electronUtil.isElectron) {
// Electron workaround
} else {
// Normal code
}
Type: boolean
Check if you're running in an Electron app.
Type: string
Example: 1.7.9
Electron version. Returns 0.0.0
when not in an Electron app.
Type: boolean
Check if the Electron app you're running in is using ASAR.
Same as the above Electron version.
- electron-store - Save and load data like user preferences, app state, cache, etc
- electron-debug - Adds useful debug features to your Electron app
- electron-context-menu - Context menu for your Electron app
- electron-dl - Simplified file downloads for your Electron app
- electron-unhandled - Catch unhandled errors and promise rejections in your Electron app
MIT © Sindre Sorhus