7

Manage Environment Variables in Node.js with dotenv

 2 years ago
source link: https://www.js-tutorials.com/nodejs-tutorial/manage-environment-variables-in-node-js-with-dotenv/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Manage Environment Variables in Node.js with dotenv

The dotenv package makes it very simple to manage environment variables for a Nodejs application. There are a number of steps involved in moving an application from dev to prod. The dotenv library, which allows us to load environment variables from a file, can be used.

Each environment has its own configuration information, which could be an token, db credentials, and so on. The separate configurations make it easier to deploy our application in different environments. We must ensure that each environment is properly configured.

Dotenv is a zero-dependency module that loads environment variables from a .env file into process.env.

Storing configuration in the environment separate from code is based on The Twelve-Factor App methodology.

What is Environment Variables

Environment variables are a subset of our application’s external variables that reside in the operating system (OS) or app running container. The environment is a collection of key and value pairs.

Ex-

DB_NAME=test

Where key is DB_NAME and value is ‘test’.

How To install dotenv in Nodejs

You can install dotenv package into nodejs application using npm OR yarn.

# with npm
npm install dotenv
# or with Yarn
yarn add dotenv

Nodejs With Dotenv

Let’s create a hello.js file. The nodejs application has some environment variables for configuration, such as hostname and port.

const http = require('http');
const hostname = process.env.HOST;
const port = process.env.PORT;
const server = http.createServer((req, res) => {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'application/json');
    res.end(`{"message": "Hello World"}`);
server.listen(port, hostname, () => {
    console.log(`Server running at http://${hostname}:${port}/`);

Node.js has a global variable process.env, It’s an object that contains all environment variables available to the user running the application.

When we run a nodejs application, we can passport and host from the command line, as shown below –
HOST=localhost PORT=3000 node hello.js

How To Integrate dotenv

Using the dotenv package, we can create configurable HOST and PORT variables. We will create a .env/code> file in the root of the nodejs app. The dotenv function loads environment variables from a .env file into the Node.js variable process.env.

We ll add below code into the top of the hello.js -
require('dotenv').config()

Create a new file .env under the same directory of your app and add the following:

HOST=localhost
PORT=3000

Now run app using command line -
node hello.js

How To Preload Environment Variables

You can use the -r command line option to preload dotenv. You do not need to require and load dotenv in your application code.

$ node -r dotenv/config your_script.js

How To Set Custom .env File Path

If your file containing environment variables is located elsewhere, you can specify a custom path.

require('dotenv').config({ path: '/full/custom/path/to/your/env_vars' })

Conclusion

The env variable refers to external variables that are stored in the OS or container in which the app is running. We can use the dotenv library to define our environment variables in a .env/code> file.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK