Reputation: 963
This is the most robust documentation I can find for the process.env
property: https://nodejs.org/api/process.html#process_process_env.
It mentions USER
, but not USERNAME
. On my machine (Windows/Bash), when I print the contents of process.env
, I see USERNAME
(my windows username) but not USER
. Similarly, echo $USERNAME
shows my name but echo $USER
returns nothing.
What is the difference between USER
and USERNAME
? Is it an operating system thing? Are they interchangeable?
Upvotes: 5
Views: 11109
Reputation: 95
I was having a similar issue when trying to connect node.js to mysql via dotenv.
None of the many answers in the web did not resolve my issue.
This worked perfectly well, without the .env file, but only with the information required for authentication inserted into the app.js file. I have tried unsuccessfully any of the posted answers, which include (but not only):
changing the information inside the .env file to be with and without ""
changing the name of the .env file
changing the path of the .env file
describing the path to .env file
writing different variations of the dotenv commands inside app.js
At last, I have tried to find if I had installed the dotenv using the npm install dotenv command. Also I have tried to show the version of the dotenv from the console.log(dotenv.MY_ENV_VAR); 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). 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. 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
.
Upvotes: 0
Reputation: 144982
The documentation about process.env
that you linked to shows an example environment; 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.
ie, a user could run
$ USER=lies node script.js
...and process.env
would not contain the real username.
If you're interested in getting information about the user your process is running as, call os.userInfo()
, which is (mostly1) consistent across platforms.
> 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
os.userInfo()
calls uv_os_get_passwd
, which returns the actual current effective user, regardless of what's in environment variables.
uv_os_get_passwd
Gets a subset of the password file entry for the current effective uid (not the real uid). The populated data includes the username, euid, gid, shell, and home directory. On non-Windows systems, all data comes from getpwuid_r(3). On Windows, uid and gid are set to -1 and have no meaning, and shell is NULL.
Upvotes: 7
Reputation: 60567
process.env
is the process's environment variables, which are supplied by the OS to the process.
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
.
Upvotes: 3