how to check whether we are running on electron or browser

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:


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:


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:


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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe