To get the current user home directory, you can use the homedir() method from the os module in Node. js. /* Get home directory of the user in Node. js */ // import os module const os = require("os"); // check the available memory const userHomeDir = os.
__dirname: It is a local variable that returns the directory name of the current module. It returns the folder path of the current JavaScript file.
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'];
}
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());
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()
).
Use osenv.home()
. It's maintained by isaacs and I believe is used by npm itself.
https://github.com/isaacs/osenv
getUserRootFolder() {
return process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With