Learn how to get information about the operative system with electron framework for hybrid apps

How to get operative system and hardware information with Electron framework

Thanks to Electron framework you can use HTML, CSS, and JavaScript with Chromium and Node.js to build your app. As a native application, you may need to access some basic information about the operative system or about the hardware if you want to show this information to the user.

In this article, you will learn how to access basic information about the hardware and the operative system in Electron.

Operative system

To get information about the operative system we will use the os module which can be loaded using :

var operativeSystemModule = require("os");

// Call any os method of os node.js here

Get networkInterfaces information

Get the information about network devices using os.networkInterfaces method.

var os = require("os");

console.log(os.networkInterfaces());

You should see a similar output :

Electron network information

Get cpu info

Retrieve the name of the name of the processor, speed etc using os.cpus method.

var os = require("os");

os.cpus();

/*
This method will return an array
with the following structure (Note that the first item in the array has been stringified which is an object with the information)
*/

{
   "model":"Intel(R) Atom(TM) CPU  Z3735F @ 1.33GHz",
   "speed":1333,
   "times":{
      "user":842156,
      "nice":0,
      "sys":576953,
      "idle":3697421,
      "irq":32656
   }
}

Get OS temporal files path

To write temporal files, you'll need to save them in the temporal files folder of the os to don't worry about delete them later, therefore use tmpDir to get the temporal files folder of the os.

var os = require("os");

os.tmpDir(); // change acording to your os and settings 

Get OS architecture

Retrive the OS architecture using the os.arch method.

var os = require("os");

os.arch(); 'x64', 'arm' and 'ia32'

Get amount of memory

Retrieve the amount of total memory in bytes from the operative system.

var os = require("os");

var bytesAvailable = os.totalmem(); // returns number in bytes
// 1 mb = 1048576 bytes
console.log("Total memory available MB :" + (bytesAvailable/1048576) );

to retrieve the number of available (free) bytes in the memory use os.freemem method.

var os = require("os");

var mbTotal = ((os.totalmem())/1048576);
var mbFree = ((os.freemem())/1048576);

console.log("There are "+mbFree+"mb free in the memory of "+mbTotal+"mb in total");

Get operative system name

You can retrieve the operative system name using os.type method and retrieve the operative system platform using os.platform

var os = require("os");

os.type(); // Linux, Darwin or Window_NT
os.platform(); // 'darwin', 'freebsd', 'linux', 'sunos' or 'win32'

The "os" module provides a few basic operating-system related utility functions, there are more useful methods that you can use reading the documentation of node.js os module here

Hard drives

To get information about the Hard drives on your system, you will need to rely on a third party library. We are talking about the drivelist module. This module list all connected drives in your computer, in all major operating systems.

Notice that this module does not require admin privileges to get the drives in any supported operating system. It supports the following platforms:

  • Windows.
  • GNU/Linux distributions that include util-linux and udev.
  • Mac OS X.

To install this module in your project, execute the following command in your terminal:

npm install drivelist

After the installation you'll be able to require the drivelist module with Javascript. The following example will list all drives of the system in the console:

const drivelist = require('drivelist');

drivelist.list((error, drives) => {
    if (error) {
        throw error;
    }

    drives.forEach((drive) => {
        console.log(drive);
    });
});

/** Console output
 
Mac OS X
[
  {
    device: '/dev/disk0',
    description: 'GUID_partition_scheme',
    size: 68719476736,
    mountpoints: [
      {
        path: '/'
      }
    ],
    raw: '/dev/rdisk0',
    protected: false,
    system: true
  }
]

GNU/Linux
[
  {
    device: '/dev/sda',
    description: 'WDC WD10JPVX-75J',
    size: 68719476736,
    mountpoints: [
      {
        path: '/'
      }
    ],
    raw: '/dev/sda',
    protected: false,
    system: true
  }
]
Windows
[
  {
    device: '\\\\.\\PHYSICALDRIVE0',
    description: 'WDC WD10JPVX-75JC3T0',
    size: 68719476736,
    mountpoints: [
      {
        path: 'C:'
      }
    ],
    raw: '\\\\.\\PHYSICALDRIVE0',
    protected: false,
    system: true
  }
]

**/

Happy coding !


Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors