Node.js - 以平台不可知的方式查找主目录 [英] Node.js - Find home directory in platform agnostic way

查看:212
本文介绍了Node.js - 以平台不可知的方式查找主目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Process.platform返回Windows的win32。在Windows上,用户的主目录可能是C:\ Users [USERNAME]或C:\ Documents and Settings [USERNAME],具体取决于正在使用哪个版本的Windows。为什么不使用 USERPROFILE 在Unix上,这不是一个问题。

解决方案 c>在win32环境变量?


$ b

function getUserHome(){
return process。 env [(process.platform =='win32')? 'USERPROFILE':'HOME'];

$ / code>

编辑:如最近的回答, https://stackoverflow.com/a/32556337/103396 是正确的路要走( require('os')。homedir())。


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.

解决方案

Why not use the USERPROFILE environment variable on win32?

function getUserHome() {
  return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
}

EDIT: as mentioned in a more recent answer, https://stackoverflow.com/a/32556337/103396 is the right way to go (require('os').homedir()).

这篇关于Node.js - 以平台不可知的方式查找主目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆