Process.platform returns "win32" for Windows. On Windows a user's home directory might be C:\Users[USERNAME] or C:\Documents and Settings[USERNAME] depending on which version of Windows is being used. On Unix this isn't an issue.
Asked
Active
Viewed 1.6e+01k times
298
-
Does Mac OS X retain the Unix method or does it require something different too? – hippietrail Dec 19 '12 at 11:47
-
3@hippietrail Mac OS X uses HOME like every other Unix. – Marnen Laibow-Koser Mar 05 '15 at 23:13
6 Answers
471
As mentioned in a more recent answer, the preferred way is now simply:
const homedir = require('os').homedir();
[Original Answer] Why not use the USERPROFILE
environment variable on win32?
function getUserHome() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}

maerics
- 151,642
- 46
- 269
- 291
-
Thank you. Just installed Node.js locally, as building a basic server on Cloud9 is not following my little tutorial (of course)... and had NO IDEA where to put my files. You are the man! (I assume) – Steve Aug 24 '13 at 08:28
-
In 0.10.33 win 64bit this does not work. I have USERPROFILE and HOME set to C: My company forces me to have a HOMEDRIVE/HOMEPATH set to a network share H:. For some reason node prefers H:. – Angus Nov 25 '14 at 19:08
-
11I have a viscerally negative reaction to this answer and the second most popular one. If you are using node.js, then IMHO, you should favor npm modules like os.homedir() and osenv.home() (both mentioned below). If you look at the source for os.homedir(), you'll see that it's not this simple. – Larry Maccherone Jan 20 '16 at 19:34
-
-
1wrapping a `path.resolve()` might provide some normalization in terms of trailing slashes, potentially odd forward/backslashes, etc... (especially if you got file operations in mind, after getting this info) – Frank N Oct 16 '17 at 12:37
-
Ah what, oh what, does one do if on a Mac? There it is `/Users/yourname`. I kinda wish this answer would just be removed. – Matt Fletcher May 17 '18 at 11:49
-
Or: `const { homedir } = require('os'); console.log(homedir());` – Jonathan.Brink May 01 '20 at 14:17
-
I used to use process.env.HOMEPATH on Windows. But then I ran the same app with administrative privileges and got an error because for some reason process.env.HOMEPATH value was undefined. So beware it is risky to rely on values of environment variables, they are not part of any standard. – Panu Logic Oct 07 '20 at 00:34
282
os.homedir()
was added by this PR and is part of the public 4.0.0 release of nodejs.
Example usage:
const os = require('os');
console.log(os.homedir());

Cody Allan Taylor
- 3,062
- 1
- 13
- 11
-
2Got it. Added sources because `os.homedir()` is a pretty darn new toy. – Cody Allan Taylor Sep 14 '15 at 03:34
-
13
83
Well, it would be more accurate to rely on the feature and not a variable value. Especially as there are 2 possible variables for Windows.
function getUserHome() {
return process.env.HOME || process.env.USERPROFILE;
}
EDIT: as mentioned in a more recent answer, https://stackoverflow.com/a/32556337/103396 is the right way to go (require('os').homedir()
).
-
1On my Windows 7 machine, HOMEPATH and USERPROFILE return the same thing (C:\\Users\\Username). HOME returns undefined. On my Mac HOME returns /Users/Username and HOMEPATH and USERPROFILE return undefined. – Ben Clayton Apr 24 '13 at 11:57
-
1on my windows 7 machine, HOMEPATH returns `\users\name` and USERPROFILE has `c:\users\name`. So this method is not as accurate and robust as @maerics' – PA. Apr 16 '15 at 15:02
16
Use osenv.home()
. It's maintained by isaacs and I believe is used by npm itself.

Andrew De Andrade
- 3,606
- 5
- 32
- 37
-
2`oshome` uses [`os-homedir`](https://www.npmjs.com/package/os-homedir) under the hood, if you want only that functionality. – spiffytech Oct 12 '15 at 19:07
4
getUserRootFolder() {
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}

aH6y
- 436
- 4
- 8
-1
in some cases try to use this:
this.process.env.USERPROFILE
or
this.nw.process.env.USERPROFILE
i.e. add this or this.nw before process

Slava Rozhnev
- 9,510
- 6
- 23
- 39