Open In App

Process Object in ElectronJS

ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.

As mentioned above, Electron combines the Chromium engine and NodeJS into a single runtime. Electron extends the features of NodeJS and provides access to several APIs which otherwise would not be accessible in a sandbox browser environment. One such crucial feature is the NodeJS process object. The NodeJS process object is a global object that provides an extensive set of information and defines the behavior of the NodeJS application. Since it is a global object, it can be accessed within any module of the NodeJS application. It provides us with an extensive set of Instance Properties, Methods, and Events that can be used.



For a detailed explanation on the Process object, refer to the article: Global, Process and buffer in Node.js. Electron also supports the global process object. It is an extension of the NodeJS process object and adds its own set of Instance Properties, Methods, and Events which are exclusive to Electron. In this tutorial, we will look at some features of the Electron’s process object.

We assume that you are familiar with the prerequisites as covered in the above-mentioned link. For Electron to work, node and npm need to be pre-installed in the system.



Example: Follow the Steps given in Drag and Drop Files in ElectronJS to set up the basic Electron Application. Copy the Boilerplate code for the main.js file and the index.html file as provided in the article. Also, perform the necessary changes mentioned for the package.json file to launch the Electron Application. We will continue building our application using the same code base. The basic steps required to set up the Electron application remain the same. 

package.json: 

{
  "name": "electron-properties",
  "version": "1.0.0",
  "description": "Process Object in Electron",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [
    "electron"
  ],
  "author": "Radhesh Khanna",
  "license": "ISC",
  "dependencies": {
    "electron": "^8.3.0"
  }
}

Output: 

Process Object in Electron: A process object is a global object which can be accessed in the Main Process as well as in the Renderer Processes. As discussed above, the process object in the Electron provides its own set of features apart from the already pre-existing features of the NodeJS process object. In a Sandbox environment, the process object contains only a subset of the APIs.

Note: The process object in Electron can interact with the System APIs as well and hence has access to detailed System properties such as CPU Information, Memory Information, etc which can be leveraged in a Desktop Application.




<h3>System Properties in Electron</h3>
  <button id="print">
    Print all System Properties in Console
  </button>

index.js: The Print all System Properties in Console button does not have any functionality associated with it yet. To change this, add the following code in the index.js file.




const electron = require('electron')
  
// Instance Properties 
process.noDeprecation = false;
process.throwDeprecation = false;
process.traceDeprecation = true;
process.traceProcessWarnings = true;
  
// Instance Event 
process.once('loaded', () => {
    console.log('Pre-Initialization Complete');
  });
  
function properties(label, value) {
    this.label = label;
    this.value = value;
}
  
var props = {};
  
var print = document.getElementById('print');
// Instance Properties, continued ...
print.addEventListener('click', (event) => {
    props.defaultApp = new properties('Default App', process.defaultApp);
    props.mainFrame = new properties('Main Frame', process.isMainFrame);
    props.resourcePath = new properties('Resource Path', process.resourcesPath);
    props.sandbox = new properties('Sandbox Environment', process.sandboxed);
    props.processType = new properties('Type of Process', process.type);
    props.chrome = new properties('Chrome Version', process.versions.chrome);
    props.electron = new properties('Electron Version', process.versions.electron);
    props.windowsStore = new properties('Window Store', process.windowsStore);
    props.CreationTime = new properties('Window Store', process.getCreationTime());
  
    console.table(props);
    // Instance Methods 
    console.log('-------------------');
    console.log('Application Creation Time');
    console.log(process.getCreationTime());
    console.log('-------------------');
    console.log('CPU Usage Information')
    console.log(process.getCPUUsage());
    console.log('-------------------');
    console.log('IOCounters Information')
    console.log(process.getIOCounters());
    console.log('-------------------');
    console.log('Heap Statistics Information')
    console.log(process.getHeapStatistics());
    console.log('-------------------');
    console.log('Blink Memory Information')
    console.log(process.getBlinkMemoryInfo());
    console.log('-------------------');
    console.log('System Memory Information')
    console.log(process.getSystemMemoryInfo());
    console.log('-------------------');
    console.log('System Versions Information')
    console.log(process.getSystemVersion());
    console.log('-------------------');
    console.log('Process Memory Information')
    process.getProcessMemoryInfo().then(processMemoryInfo => {
        console.log(processMemoryInfo);
    }).catch(err => {
        console.log(err);
    })
});

Instance Events:

Instance Properties:

 

Instance Methods:

Output: 


Article Tags :