Configuring Environment Variables, Git and Reloading Automatically with Nodemon | Nodejs

Jawaria Noman
5 min readSep 27, 2020

--

Throughout this article we will go through how to setup environment variables in nodejs, how we can reload the nodejs app on saving changes with nodemon and lastly we will see how can we push our project to github. If you are new and don’t know how to setup a nodejs project, I have a detailed article on that in the link below. Go through it and then come back here.

Environment variables

Environment variables are the dynamic variable which can be used globally within the project. Mostly they are used for configuration like an account or database etc. Following is the purpose of using global variables:

  • Global access
  • Different environment have different variables like development environment have different configuration variables then production.
  • Security, pushing configuration variables on git can be vulnerable. So environment variable file is kept locally instead of pushing on git (Don’t worry this is explained at the end of article).

As in example PORT that we set for our app to run on can be an environment variable because it can be different on development or production environment.

In nodejs we will use dotenv package to use the environment variables.

dotenv

dotenv package is used to manage our environment variables in our node application. We can put key information in there instead of using it repeatedly in different files.These variables will be available to access in every file and module

To install dotenv in our app, simply write the following command in your terminal

npm i dotenv

Now, in your project root directory create a file called .env

Add your variables in .env file. Use 1 line for each variable. Now lets add the PORT variable into .env file.

PORT=4000

Go to you app.js file, import the dotenv package and configure.

require('dotenv').config()

Now that it is configured lets get our port in app.js

const port= process.env.PORT

Here is full app.js code from previous tutorial with .env

require('dotenv').config()const express= require('express')
const bodyParser= require('body-parser')
const app=express()
const port= process.env.PORTapp.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true;
}))
app.get('/', (req, res )=> {
res.json({app: 'The end point is working'})
})
app.listen(port, () => {
console.log(`Successful! The server is up and running on port ${port}`)
})

Run and you will see that the project is running on port you mentioned in .env file.

Nodemon

Till now whenever we make any changes to our app we have to close the app.js and then start again manually to see those changes. It gets more difficult when we are developing an app with a lot of functions, considering every time we make any small change we’ll have to restart our server to see the changes. We can skip restarting the server continuously by installing nodemon in our app.

Nodemon is a tool for development environment that detects any changes that are made, and automatically restarts the server.

Installing Nodemon

We can install nodemon by using the following command in development dependency

— save-dev installs it development dependencies

npm i --save-dev nodemon

Now go to your package.json file and add the following line in your script object, it should look like this

"scripts": {
"start": "nodemon app.js",
},

Now go to terminal and write

npm start

You will see the server is up and running. Now make changes in your app.js file, save and you will see that nodemon will automatically restart the server.

Git

Git is version control system which will let you track your changes in your code during the development process. You can leave messages for every change you do so you can track it later on.

We will push this project to github. It is free for personal use. Install the git from here and follow the rest of the tutorial.

.gitignore

.gitignore is a file in which each line contains a file name to ignore. If you want that some of your files should not be pushed to your online repository you should add their name in this file. Remember we were talking about how .env file should not be pushed to online repository.

Create a .gitignore file in your project root directory and add our .env and node_modules file so these files don’t get uploaded on your repository.

.env
node_moduls

Now lets push it onto github repository. Create a public/private repository here and follow the tutorial.

Github project upload

Now write this the following commands in cmd

  1. First create a git repository in root directory with git init. This will display a message “Initialized empty git repository in ‘path’ ”.
  2. Now we need to stage all our files in the repository we want to push. As we have to push the complete project we will stage all of project with following command. Type git add . in your command line
  3. We have created the repository and stage all our files to it. Now we need to commit these changes with a message so we can track them. Type git commit -m “Initial Commit” in command line. -m allows you to add a message for the commit.
  4. Now we have to push our changes to our repository so we will instruct git to add a remote repository we created in the previous step on github so whenever we make any changes and push it, it goes directly to that origin. Type git remote add origin your-repository-url in your command line
  5. Now it is time to finally push(upload) our commit in the repository. Type this command to push new commit git push -u origin master

Go to your repository on github and you will see your code there. About public/private, if your repository is public anyone can access it but if its private only you can access it. Always use private repo for your professional use.

Phew!!! Now we are done with this tutorial. If you have any question leave a response and we will try to sort it out.

--

--