Summary: in this tutorial, you’ll learn how to read the environment variables using the Node.js process.env
property of the process
module.
Introduction to the node.js process module
The process
is a core module of Node.js. The process
module has the env
property that contains all the environment variables.
To set an environment variable on Windows, you use the SET
command. For example:
SET NODE_ENV=development
Code language: JavaScript (javascript)
On macOS or Linux, you use the EXPORT
command:
EXPORT NODE_ENV=development
Code language: JavaScript (javascript)
In this syntax, the NODE_ENV
is an environment variable with the value 'development'
.
To get access the NODE_ENV
in Node.js, you use the process.env
like this:
process.env.NODE_ENV
Code language: JavaScript (javascript)
In this example, the proces.env.NODE_ENV
returns the string 'development'
.
If the NODE_ENV
is not set, the process.env.NODE_ENV
will return undefined
.
The following statement uses the logical OR operator to return the current Node.js environment or development
if the NODE_ENV
is not set:
const nodeEnv = process.env.NODE_ENV || 'development';
Code language: JavaScript (javascript)
It’s a good practice to store credential information such as database username and password in the environment variables.
Let’s see some practical examples of using the Node.js process.env
.
Using environment variables to store credential information securely
The following example shows you how to use the process.env
to access the current Node.js environment and load the configuration accordingly.
The project structure will look like this:
├── config
| ├── app.js
| ├── development.js
| └── production.js
├── index.js
└── package.json
Code language: plaintext (plaintext)
First, create a new folder named env
.
Second, run the npm init
command to initialize a new package:
npm init --yes
Code language: JavaScript (javascript)
Third, create a new folder called config
inside the project root directory:
mkdir config
Code language: JavaScript (javascript)
Fourth, create three files app.js,
development.js
, and production.js
in the config
folder. The development.js
stores the database credentials in the development environment:
module.exports = {
database: {
host: "localhost",
port: 27017,
username: "admin",
password: "Password1",
},
};
Code language: JavaScript (javascript)
The production.js
file stores the same information as the development.js
. However, it uses the environment variables instead:
module.exports = {
database: {
host: process.env.DB_HOST,
port: process.env.DB_PORT,
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD,
},
};
Code language: JavaScript (javascript)
You should set the environment variables DB_HOST
, DB_PORT
, DB_USERNAME
, and DB_PASSWORD
in the production.
Note that if you store the confidential information in the production.js
file and commit it to a public repository, you’ll put the application’s security at risk.
The app.js
will require the production.js
or development.js
, depending on the current Node.js environment:
let config;
const nodeEnv = process.env.NODE_ENV || "development";
if (nodeEnv === "devlopment") {
config = require("./development.js");
} else {
config = require("./production.js");
}
module.exports = config;
Code language: JavaScript (javascript)
Fifth, load the configuration information from the app.js
in the index.js
file:
const config = require("./config/app.js");
console.log(config);
Code language: JavaScript (javascript)
Using the environment variables to control which code to run
Sometimes, you may want to execute some code in the development environment but not in the production environment.
For example, you may want to show debugging information in the development environment but not in the production environment.
To do that, you can use the process.env
to access the DEBUG
environment variable. For example:
First, create a new module called debug.js
and define a function that logs information only when the DEBUG
is set:
module.exports = (data, level = "info") => {
if (!process.env.DEBUG) return;
switch (level) {
case "info":
console.info(data);
break;
case "error":
console.error(data);
break;
case "warning":
console.warn(data);
break;
default:
console.log(data);
}
};
Code language: JavaScript (javascript)
Second, load the debug.js
to the index.js
file:
const debug = require("./debug");
Code language: JavaScript (javascript)
Third, show some debug information to the console:
const debug = require("./debug");
debug("The app is starting....");
Code language: JavaScript (javascript)
If you set the DEBUG
environment variable to true, you’ll see the output.
Summary
- The
process
is a core module of Node.js. Theprocess.env
property holds all the environment variables. - Store confidential information in the environment variables and use the
process.env
to access them.