How to Use Environment Files (.env) in Node.js - Tutorial (dotenv)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey how's it going guys my name is dom and today i'm going to be showing you how to use dot m files in node js okay so first i want to take you through uh what these dot m files are and why you should use them okay so right here i prepared a list earlier going over a couple of things so the first question is what exactly is a dot m file well basically you can use n file to store environment specific configuration or settings for your application okay so think about things like database credentials urls for accessing third-party information or resources and even something like an api key for small you know personal applications and things like that okay now the main benefit for you know using a end file is to avoid hard coding those things like settings and api keys database credentials things like that so it's a way for you to avoid hard coding those environment specific things so even if you were to publish your application to a web hosting service things might be different on there compared to your local environment therefore you know put that data in a file instead it makes your application supported on more or you know more environments than just your local development environment okay now let's just say for example you've got a friend who wants to use your weather application so your weather application might need something like which country or city to fetch weather for as well as an api key to actually access the api to retrieve the weather information so those two things right there the location and the api key your friend might have a different api key and a different city therefore store it in the end file and your application will run there perfectly fine so that is the main motivation behind using a dot m file so when it comes to node.js how do you get started in using one well right here i've got a empty directory so i'm going to make a new node.js project i can say npm init dash y and now we have a new node.js project so now i just want to install the dot m package okay so right here i'll say npm i then dot m just like that and press enter and of course now it's going to install this node.js package all right so now in order to use of course the ember package we're going to need to actually create a dot m file so let's make a new file here called dot m then inside here i'm just going to store something like an api key so going back to the weather example i can say something like weather api key equal to then00d32 just as an example okay cool so right here we have this environmental variable stored inside my dot m file so now how do i access this value in my node.js application well if i make a new index.js right here and then at the very top i can now require dot m but i can actually do it in a in a bit of a different way so i can actually just say right here require just like normal and then i can say dot m and then right after it i can say dot config so right here by calling dot config it's gonna read your dot m file okay and then it's gonna make your data available in process dot env oops let me try and spell this correctly there we go so process.m is going to contain your environmental variables so i can now say something like console.log then i'll say process.env.weather api key i can now run the application i'll say node then index.js as we can see right here we get a weather api key inside the console all right so that is basically how you're able to access your environmental variables now you may notice that when pulling down a repository from github something like that you may notice that the uh the code actually comes with something like a dot m then dot example so if you encounter the dot m dot example this right here is just trying to assist you with creating a dot m file so you're gonna want to essentially copy this example then paste it and then rename this to be dot m and then you're going to want to put your exact environmental you know settings or configuration inside that one so this is just simply meant to be like a a bit like a template for you to then fill in your data for your specific environment so um that is uh using environmental variables in node.js thanks for watching guys if you like this video drop a like and subscribe and i'll see you guys in the next one
Info
Channel: dcode
Views: 136,411
Rating: undefined out of 5
Keywords: nodejs tutorial, nodejs env files, node env files, nodejs environment setup, nodejs environment variables, how to use .env files, what are .env files in programming, .env files tutorial, environmental variables tutorial
Id: hZUNMYU4Kzo
Channel Id: undefined
Length: 5min 26sec (326 seconds)
Published: Wed Dec 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.