Node.js - Find home directory in platform agnostic way

后端 未结 5 989
我寻月下人不归
我寻月下人不归 2020-11-28 01:56

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 v

5条回答
  •  春和景丽
    2020-11-28 02:23

    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()).

提交回复
热议问题