简体   繁体   English

Node 中的 `process.env.USER` 和 `process.env.USERNAME` 有什么区别?

[英]What is the difference between `process.env.USER` and `process.env.USERNAME` in Node?

This is the most robust documentation I can find for the process.env property: https://nodejs.org/api/process.html#process_process_env .这是我能找到的最强大的process.env属性文档: https://nodejs.org/api/process.html#process_process_env

It mentions USER , but not USERNAME .它提到USER ,但没有提到USERNAME On my machine (Windows/Bash), when I print the contents of process.env , I see USERNAME (my windows username) but not USER .在我的机器 (Windows/Bash) 上,当我打印process.env的内容时,我看到了USERNAME (我的用户名 windows),但没有看到USER Similarly, echo $USERNAME shows my name but echo $USER returns nothing.同样, echo $USERNAME显示我的名字,但echo $USER什么也不返回。

What is the difference between USER and USERNAME ? USERUSERNAME有什么区别? Is it an operating system thing?是操作系统的事吗? Are they interchangeable?它们可以互换吗?

The documentation about process.env that you linked to shows an example environment; 有关您链接的process.env的文档显示了一个示例环境; it is not meant to be normative. 它并不意味着规范。 process.env can be basically anything -- its values generally have OS defaults provided by the shell, but ultimately they are controlled by the user and/or the process that launched your process. process.env基本上可以是任何东西 - 它的值通常具有shell提供的操作系统默认值,但最终它们由用户和/或启动进程的进程控制。

ie, a user could run 即,用户可以运行

$ USER=lies node script.js

...and process.env would not contain the real username. ...而process.env不包含真实的用户名。


If you're interested in getting information about the user your process is running as, call os.userInfo() , which is (mostly 1 ) consistent across platforms. 如果您有兴趣获取有关您的进程正在运行的用户的信息,请调用os.userInfo() ,它在各个平台上(大多数是1 )一致。

> os.userInfo()
{ uid: -1,
  gid: -1,
  username: 'josh',
  homedir: 'C:\\Users\\josh',
  shell: null }

1 - on Windows, uid , gid , and shell are useless, as seen above 1 - 在Windows上, uidgidshell都没用,如上所示

os.userInfo() calls uv_os_get_passwd , which returns the actual current effective user, regardless of what's in environment variables. os.userInfo()调用uv_os_get_passwd ,它返回实际当前有效用户,无论环境变量是什么。

uv_os_get_passwd Gets a subset of the password file entry for the current effective uid (not the real uid). uv_os_get_passwd获取当前有效uid(不是真实uid)的密码文件条目的子集。 The populated data includes the username, euid, gid, shell, and home directory. 填充的数据包括用户名,euid,gid,shell和主目录。 On non-Windows systems, all data comes from getpwuid_r(3) . 在非Windows系统上,所有数据都来自getpwuid_r(3) On Windows, uid and gid are set to -1 and have no meaning, and shell is NULL . 在Windows上,uid和gid设置为-1并且没有任何意义,shell为NULL

process.env is the process's environment variables , which are supplied by the OS to the process. process.env是进程的环境变量 ,由OS提供给进程。

This object can really contain just about anything, as specified the OS and the process that launches it, but by default Windows stores the username in USERNAME and Unix-like systems (Linux, macOS, etc.) store it in USER . 该对象实际上可以包含任何内容,如指定操作系统和启动它的进程,但默认情况下,Windows将用户名存储在USERNAME和类Unix系统(Linux,macOS等)中,将其存储在USER

I was having a similar issue when trying to connect node.js to mysql via dotenv.尝试通过 dotenv 将 node.js 连接到 mysql 时,我遇到了类似的问题。

None of the many answers in the web did not resolve my issue. web 中的许多答案都没有解决我的问题。

This worked perfectly well, without the.env file, but only with the information required for authentication inserted into the app.js file.这工作得很好,没有 .env 文件,但只有将身份验证所需的信息插入到 app.js 文件中。 I have tried unsuccessfully any of the posted answers, which include (but not only):我尝试过任何已发布的答案均未成功,其中包括(但不仅限于):

  1. changing the information inside the.env file to be with and without ""将 .env 文件中的信息更改为带和不带“”

  2. changing the name of the.env file更改 .env 文件的名称

  3. changing the path of the.env file改变.env文件的路径

  4. describing the path to.env file描述 .env 文件的路径

  5. writing different variations of the dotenv commands inside app.js在 app.js 中编写 dotenv 命令的不同变体

At last, I have tried to find if I had installed the dotenv using the npm install dotenv command.最后,我尝试查找是否使用 npm install dotenv 命令安装了 dotenv。 Also I have tried to show the version of the dotenv from the console.log(dotenv.MY_ENV_VAR);我也试图从 console.log(dotenv.MY_ENV_VAR); 显示 dotenv 的版本; which again, showed undefined.再次显示未定义。

The issue was related to the fact, that dotenv confused USER (of the system, again like you I was using Linux) with USERNAME (of the mysql database).这个问题与一个事实有关,dotenv 将 USER(系统的,再次像你一样,我使用的是 Linux)与 USERNAME(mysql 数据库的)混淆了。 Actually USER returns the current system user instead of the mysql database user, which I have set to USERNAME in the.env file for convenience.实际上 USER 返回当前系统用户而不是 mysql 数据库用户,为了方便,我在 .env 文件中将其设置为 USERNAME。 Now it was able to connect to the database!现在它可以连接到数据库了!

To check this, you could use:要检查这一点,您可以使用:

console.log(process.env.USER);

and:和:

console.log(process.env.USERNAME);

1st gives you the system user, whereas the 2nd gives the database user.第一个给你系统用户,而第二个给数据库用户。

Actually, any name for the variable, that holds the username of the mysql database could be used, as far as it does not match the reserved name for the system username in Linux, which is USER .实际上,可以使用包含 mysql 数据库用户名的任何变量名称,只要它与 Linux 中系统用户名的保留名称USER不匹配即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM