1

I'm currently building a electron app an building the installer like .deb and .snap with electron-builder. Inside the app I need the access the user home directory for some reason and I'm using process.env.HOME || process.env.USERPROFILE to get the home directory (also app.getPath('home') returns the same). This works properly for other cases but when the app installed with .snap, instead of the actual home(/home/username) directory this returns ~/snap/<app_name>/3/ as home directory. (3 is the snap revision number)

How do I get the actual home directory(/home/username) in all cases ?

Christian Fritz
  • 20,641
  • 3
  • 42
  • 71
Eular
  • 1,707
  • 4
  • 26
  • 50
  • Hey there! If your question was answered, please consider upvoting/accepting using the green checkmark next to the answer. – snwflk Feb 02 '21 at 15:01

1 Answers1

0

Note that ~/ is not a valid path; it is expanded to $HOME by the shell. The actual path you're looking for will be /home/username in most cases. A simplistic solution would be to simply assume that to be the home dir.

To safeguard against situations where the home folder is not at this location, we should ask the operating system:

os module

const os = require("os");

const user_name = os.userInfo().username;
const user_dir = os.userInfo().homedir;

console.log(user_name, user_dir);

From the NodeJS docs:

The value of homedir returned by os.userInfo() is provided by the operating system. This differs from the result of os.homedir(), which queries environment variables for the home directory before falling back to the operating system response.

posix module

(original answer) If for some reason the above does not work, you can explicitly use getpwnam to look up the actual home directory from /etc/passwd:

const posix = require("posix");
const os = require("os");

const user_name = os.userInfo().username;
const user_dir = posix.getpwnam(user_name).dir;

console.log(user_name, user_dir);

You will need to install the posix module first:

npm install posix
snwflk
  • 3,341
  • 4
  • 25
  • 37
  • when I use the `showSaveDialog` (https://www.electronjs.org/docs/api/dialog) it opens up a nautilus like file explorer (in ubuntu). If I click on the `Home` tab then it opens up the `~/snap//3/`. For new users its getting confusing as it shows as `Home` but it doesn't openup the user's home. Is there any way to fix that? – Eular Jan 19 '21 at 10:42
  • @Eular, that seems like a separate (and interesting) question! I believe it should be its own SO question, and not live hidden in a comment! – snwflk Jan 19 '21 at 14:24
  • added as extra part for the question – Eular Jan 20 '21 at 17:59
  • @Eular, your editing the question in a substantial way means invalidating a previously fitting answer and the effort that has gone into it. Your original question does not in any way mention the save/open dialog, an addition that poses an entirely different problem. I strongly advise you to roll back the edit and *post a new question* where you ask what seems to be your actual problem, not modify the existing one which was answered! – snwflk Jan 21 '21 at 02:16
  • done @ https://stackoverflow.com/questions/65872077/electron-builder-snap-installer-confusing-home-directory – Eular Jan 24 '21 at 14:57