Hello Guys, Today I will explain how to access the Environment variable in the Node js project. Working with environment variables is a great way to configure different aspects of your Node.js application. Many cloud hosts viz. AWS, GCP, Heroku, Azure, etc. by default allows us to use environment variables, we can access them from UI or by using Node.js modules. Here one of the use cases for the cloud hosts we can set an environment variable named PORT which allows us specifies a value that corresponds to port number the server should listen in order to work properly.
What blog covers?
- What is the Environment variable?
- Why use an Environment variable?
- How to use the Environment variables in the project?
- Explain the dotenv package
Deploying an application requires developers to put thought and consideration into how it is configured. Many apps are deployed in a development environment before being deployed to the production environment. We need to ensure each environment is configured correctly, it could be disastrous if our production application was using our development database, for example.
Environment variables allow us to manage the configuration of our applications separate from our codebase. Separate configurations make it easier for our application to be deployed in different environments.
- Node installed on your machine
- NPM installed on your machine
- Create Node Application into your system
- Install Node packages like express
- Create Backend Server in index.js file using the express package
What is the Environment variable?
Environment variables are variables external to our application which reside in the OS or where the container of the app is running in. An environment variable is simply a name mapped to a value.
By convention, the name is capitalized e.g.PORT=3000. The values are strings. If you open the terminal or command line application in Linux, Mac OS, or Windows and enter set, you will see a list of all environment variables for your user.
Why use an Environment variable?
Environment variables are excellent for decoupling application configurations. Typically, our applications require many variables to be set in order for them to work. By relying on external configurations, your app can easily be deployed in different environments. These changes are independent of code changes, so they do not require your application to be rebuilt to change.
Data that changes depending on the environment your app is running on should be set as environment variables.
Some common examples are:
HTTP Port and Address (PORT=3040)
Database, cache, and other storage connection information
Location of static files/folders
For example, in a development environment, your app will point to a test API URL, whereas in a production environment your app will point to the live API URL.
Sensitive data like API keys should not be in the source code or known to persons who do not need access to those external services.
Environment Variables in Node.js
Accessing environment variables in Node.js is supported right out of the box. When your Node.js process boots up it will automatically provide access to all existing environment variables by creating an env object as a property of the process global object. If you want to take a peek at the object run the Node.js.
I hope you have install dependencies and create a node project. create a .env file
In your project root directory and set an Environment variable. Example here
.env // environment file name
PORT=4000 PATH=/home/data/images/ // location of static directory or file HOST=http://localhost DATABASENAME=test_dev
After you set the Environment variable, now your question is how can access that environment variable? It easy to access the variable in your project. Example
process.env.PORT process.env.PATH process.env.ACCESS_KEY process.env.DATABASENAME
If you can not access the environment variable by using process.env so you need to use the dotenv library and access the environment variable.
You can install a dotenv package using a command.
$ npm install dotenv --save
After creating .env file and set the Environment variable. Example .env file above
I hope you have to create .env file and set the environment in a file. You can load the package dotenv in the js file.
Index.file // your node file
You need to load the dotenv package in your project and access the environment variable in your need variable. Same as the above example here.
var port = normalizePort(process.env.PORT || '3040');//use PORT env variable app.set('port', port);
Environment variables exist outside our application's code, they are available where our application is running. They can be used to decouple our application's configuration from its code, which allows our apps to be easily deployed across different environments.
With Node.js apps, environment variables are available through the process.env global variable. We can set the environment variables before we run the node command.
we can use the dotenv library which allows us to define our environment variables in .env file.
.env file should never be in the source code repository.
We are a team of expert developers, testers and business consultants who strive to deliver nothing but the best. Planning to build a completely secure and efficient node app? 'Hire Node Developers'.
Third Rock Techkno is a leading IT services company. We are a top-ranked web, voice and mobile app development company with over 10 years of experience. Client success forms the core of our value system.
We have expertise in the latest technologies including angular, react native, iOs, Android and more. Third Rock Techkno has developed smart, scalable and innovative solutions for clients across a host of industries.
Our team of dedicated developers combine their knowledge and skills to develop and deliver web and mobile apps that boost business and increase output for our clients.