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.
This question is related to
node.js
filesystems
platform-independent
home-directory
platform-agnostic
getUserRootFolder() {
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}
Use osenv.home()
. It's maintained by isaacs and I believe is used by npm itself.
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()
).
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());
Source: Stackoverflow.com