'os.platform returns browser instead of actual OS - React & Electron

I am trying to retrieve the appdata folder location for the application, and since each os has a separate path for the appdata or application support folder, I tried retrieving the os type to specify which path to use. The issue is os.platform() returns 'browser'. I have tried running it on windows and mac, but they all return browser. If i run process.platform in electron.js it gives me the correct os, but in react I get browser. How can I get the proper OS?



Solution 1:[1]

In a browser you can use a combination of navigator.platform, navigator.userAgent, and navigator.userAgentData.platform to get the information you want, but it might take some testing and parsing.

AFAIK, navigator.userAgentData.platform is available only on Chrome/Chromium-based browsers, but gives the most straight-forward result when available.

Checking which platform you're using, rather than checking for specific features, is generally consider not to be a good idea -- but I've found it hard to avoid sometimes myself, especially when working around platform-specific quirks and bugs.

Solution 2:[2]

This is because you are running process.platform in the renderer process.

In order to get the correct value you need to run platform.process either on main process (usually the background.js file) or via @electron/remote, like this:

window.require('@electron/remote').process.platform

@electron/remote's usage depends on your electron version, I recommend you to check @electron/remote readme.

Solution 3:[3]

Have you tried using Electron's app.getPath(name) method?

This method will return the users application data directory.

Only once the app is "ready" can you retrieve this path.

// Electron module(s).
const electronApp = require('electron').app;

// Application is now ready to start.
electronApp.on('ready', () => {

    // Get the users app data directory.
    let appData = electronApp.getPath('appData');

    // Get the users home directory.
    let home = electronApp.getPath('home');
})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kshetline
Solution 2 Selk'nam
Solution 3 midnight-coding