Search code examples
node.jsfilesystemsplatform-independenthome-directoryplatform-agnostic

Node.js - Find home directory in platform agnostic way


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.


Solution

  • 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'];
    }