ChatGPT解决这个技术问题 Extra ChatGPT

Read environment variables in Node.js

Is there a way to read environment variables in Node.js code?

Like for example Python's os.environ['HOME'].


M
Michał Perłakowski
process.env.ENV_VARIABLE

Where ENV_VARIABLE is the name of the variable you wish to access.

See Node.js docs for process.env.


Note that this will not be visible outside the node process and its subprocesses. E.g. it wouldn't be visible if you fire env in another shell window while the node process is running, nor in the same shell after the node process exits.
this also works for assigning variables. process.env.FOO = "foo"; works.
It's worth mentioning that this does not work in a React application. process.env is sanitized for security reasons. Only variables that begin with REACT_ENV_ are available. See: github.com/facebookincubator/create-react-app/blob/master/…
@MarkEdington I think it should be REACT_APP_
@Mr.14 Right you are! It's REACT_APP_ not REACT_ENV_
C
Community

When using Node.js, you can retrieve environment variables by key from the process.env object:

for example

var mode   = process.env.NODE_ENV;
var apiKey = process.env.apiKey; // '42348901293989849243'

Here is the answer that will explain setting environment variables in node.js


what lib is required to use the above process.env method?
@user_mda process.env is built into the node.js api.
Do I just set whatever I want on the process.env? why do people set it there as opposed to say, a config object that is require()'ed by node.js?
process.env gives you access to environment variable set at an operating system level. These can be set in various ways and will depend on where you are deploying your app For example, I often run my local app using NODE_ENV=development NODE_PATH=lib node server.js. Then process.env.NODE_PATH will return 'lib'
P
Peter Mortensen

If you want to use a string key generated in your Node.js program, say, var v = 'HOME', you can use process.env[v].

Otherwise, process.env.VARNAME has to be hardcoded in your program.


Why do you have process.env['HOME']?
I
Igor Litvinovich

To retrieve environment variables in Node.JS you can use process.env.VARIABLE_NAME, but don't forget that assigning a property on process.env will implicitly convert the value to a string.

Avoid Boolean Logic

Even if your .env file defines a variable like SHOULD_SEND=false or SHOULD_SEND=0, the values will be converted to strings (“false” and “0” respectively) and not interpreted as booleans.

if (process.env.SHOULD_SEND) {
 mailer.send();
} else {
  console.log("this won't be reached with values like false and 0");
}

Instead, you should make explicit checks. I’ve found depending on the environment name goes a long way.

 db.connect({
  debug: process.env.NODE_ENV === 'development'
 });

I like to use 'yes' and 'no' for boolean env vars which must be explicitly checked. This avoids problems in many programming languages.
H
Huy Vo

You can use env package to manage your environment variables per project:

Create a .env file under the project directory and put all of your variables there.

Add this line in the top of your application entry file: require('dotenv').config();

Done. Now you can access your environment variables with process.env.ENV_NAME.


The dotenv package is useful, but the question asked is answered by reading process.env.ENV_VARIABLE. The dovenv package is all about loading setting from a file into the environment.
That's my point: manage and load env variables from env library.
You can do the same in a non-node.js-specific way using the envdir utility.
Similar to github.com/bitrise-io/envman which is also not node-specific.
This should be the answer. I was trying just with the process.env.MY_VAR and wouldn't work until I put the requre sentence. Thanks!!
J
Juanma Menendez

If you want to see all the Enviroment Variables on execution time just write in some nodejs file like server.js:

console.log(process.env);


S
Sahil Thummar

Using process.env. If Home is your env variable name then Try this:

const HOME = process.env.HOME;

Or

const { HOME } = process.env;

J
John Doe

Why not use them in the Users directory in the .bash_profile file, so you don't have to push any files with your variables to production?


The reason is because if you use .bash_profile then it will set it for that user's environment but if you are running multiple instances there then you have to set multiple env variables for that rather than having single one. E.g. if you set PORT variable then you have to make it like PORT_1, ... but if you use it through .env then you can use same code with .env file having different PORT number.
I don't see how you set a variable in a .bash_profile it's any different than a .env. It's SAFER when an app is hosted on a linux server in the .bash_profile with the same variables you would set on a .env file. You can still use different PORT number in a bash_profile based on conditions and again it's also safer.