Node.js - Find home directory in platform agnostic way

后端 未结 5 990
我寻月下人不归
我寻月下人不归 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:33

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

提交回复
热议问题