Skip to content
the poor coder

how to check whether we are running on electron or browser

Poor Man
Published
Reading time
2 min read
On this page

How to check whether we are running on Electron or Browser#

If you are developing a desktop application using Electron, there may arise situations where you need to run different code based on whether your application is running on Electron or a browser. Here are a few ways to check whether your code is running on Electron or a browser:

Using the process object#

The process object is a global object that provides information about the current node process. In an Electron application, the process object has an additional property called "type" that can be used to determine whether the code is running on Electron or a browser. Here's how you can do it:

JavaScript

if (process.type === 'renderer') {
  console.log('Running on Electron');
} else {
  console.log('Running on Browser');
}

Using the renderer process#

In an Electron application, there are two types of processes - the main process and the renderer process. The main process runs in the background and manages all the windows while the renderer process runs in the foreground and handles the UI. In a renderer process, you can use the "process" global object to check whether the code is running on Electron or a browser. Here's how you can do it:

JavaScript

if (typeof process !== 'undefined' && typeof require !== 'undefined') {
  console.log('Running on Electron');
} else {
  console.log('Running on Browser');
}

Using the window object#

You can also use the window object to check whether the code is running on Electron or a browser. In an Electron application, the window object has an additional property called "process" that can be used to determine whether the code is running on Electron or a browser. Here's how you can do it:

JavaScript

if (typeof window.process !== 'undefined') {
  console.log('Running on Electron');
} else {
  console.log('Running on Browser');
}

These are a few ways to check whether your code is running on Electron or a browser. You can use any of these methods depending on your use case.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC