1. Home
  2. Training Library
  3. Programming
  4. Programming Courses
  5. Introduction to Express - DotEnv for Environment Variables

Introduction to Express - DotEnv for Environment Variables

Contents

keyboard_tab
Introduction to Express - DotEnv for Environment Variables
Introduction to Express - Dotenv for Environment Variables
Difficulty
Beginner
Duration
4m
Students
5
Description

In this course, you will learn how to install and implement the dotenv NPM package to use environment variables inside your express application. Dotenv attaches environment variables as properties inside of the process.env object.

Learning Objectives

  • Install dotenv package
  • Implement a .env file
  • Learn what dotfiles are in general
  • Import the dotenv package inside of an Express package
  • Use environment variables inside of the Express package

Intended Audience

  • This course is intended for anyone who wants to learn about Express

Prerequisites

Transcript

Introduction to Express Dotenv for Environment Variables. In this video, I'm going to cover how to handle environment variables in the Express application using the dotenv npm package. To begin in the terminal, I will type npm install dotenv. In order to use dotenv, I need to create a dotfile for it. I will click on 'New File', the file name begins with a period, env. Now dotfiles could be configuration files or other files and they all begin with a period. By default, dotfiles are hidden by your operating system. Now for this demonstration, I'm going to set the port used by this Express application as an environment variable. Inside of the dotenv file, I will type port = 3001. Now, one thing about environment variables is they are meant to be secure. I do not want to include them as part of this Git repository. I'm going to create a Git ignore file. Once again, new file beginning with a dot gitignore. Inside of the file I will add dotenv.

Now Git will ignore this file as part of any commits. Next, I need to go to the server.js file and configure dotenv. In order to make this work, I need to require the dotenv package and execute its convict method. Near the top, I will type require.env.config. Note I'm not setting the require statement equal to a variable, I'm just executing the package. Earlier when I created the dotenv file, I set the port as an environment variable. I'm going to change the port variable here in the server file to be equal to that environment variable. I'm going to replace 3001 with process.env.PORT, and port is in all caps to match how it was written in the dotenv file. So, what is this process.env.PORT that is attached to the port environment variable? 

I'm going to dive a little deeper into node in the process object. The process object provides information about and control over the current NodeJs process. As part of this process object, there's also a sub-object property known as env. Process.env provides a user's environment. One can figure the dotenv npm package will attach the information inside of the dotenv file as additional properties to process.env. Returning back to the server.js file, after the process.env.PORT environment variable, I'm going to add the OR operator and provide a secondary value of 4000. I'm using the OR operator as part of this declaration as a fail-safe. If for some reason the environment variable doesn't exist or is undefined, the 4000 will become the new port value. I'm only using 4000, in this case, to differentiate this value from 3001.

The port number you use is whatever works for your development environment. Now, this is ready to test out. So, down below in the terminal, I will type node server.js and hit 'Enter' and the server has started on the terminal running on port 3001. So, this works. And that's it, thanks for watching at Cloud Academy.

 

About the Author
Students
4009
Labs
24
Courses
61
Learning Paths
24

Farish has worked in the EdTech industry for over six years. He is passionate about teaching valuable coding skills to help individuals and enterprises succeed.

Previously, Farish worked at 2U Inc in two concurrent roles. Farish worked as an adjunct instructor for 2U’s full-stack boot camps at UCLA and UCR. Farish also worked as a curriculum engineer for multiple full-stack boot camp programs. As a curriculum engineer, Farish’s role was to create activities, projects, and lesson plans taught in the boot camps used by over 50 University partners. Along with these duties, Farish also created nearly 80 videos for the full-stack blended online program.

Before 2U, Farish worked at Codecademy for over four years, both as a content creator and part of the curriculum experience team.

Farish is an avid powerlifter, sushi lover, and occasional Funko collector.