Node.js - Find home directory in platform agnostic way
Solution 1:
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'];
}
Solution 2:
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());