 What is going on everybody my name is Roddy and you're watching my channel Roddy the Brand today we're going to explore the .EMV MPM package which is a zero dependency module that loads environment variables from a .EMV file into process.EMV and if you're not familiar with process.EMV it's basically a global variable that is injected by nodes at runtime in a app and it represents the state of the system environment when the app starts. The most common use of .EMV file is to store database connection details, execution modes such as development predictions, tagging, API endpoints, application key, paths and port numbers. Before I begin if you like this video hit the like button it would help me a lot consider subscribing for more videos like this and if you have any questions please comment below and now let's jump on the computer and get started. Hey welcome everybody and let's get started. So first of all I'm going to do everything from beginning so I'm going to create a brand new project and I'm going to show you how we can use the .EMV. If you wish to skip feel free to do so but if you need to know JS you can follow along. So first of all I have my .EMV folder here dash node which is my project folder so create the brand new folder and here is where our project will live. So first of all we need to initialize a new project and to do this we can click left shift right click on windows and to open the power share window here and this is basically going to CD to our project folder. If you're on Mac or Linux I'm not sure whether there is a shortcut for this but all we have to do is CD to your project folder and now we can initialize a new project. To do this there are two ways we can do npm init and press enter and this is going to ask us quite a few questions about the project or you can skip this by doing the y flag dash y and press enter. This is going to create the package.adjacent file for us and we should be good to go. Now the first thing that we need to do in here is to install .EMV dependency so we can start using it. To do this we can we might as well do it now so we can do npm install and then .EMV. This should take a second and as you can see the package is now installed and I quickly want to show you the official website so if you go to npmjs.com.packages.emv this is the official documentation and they have tons of examples of how you can use it how you can still it and so on but yeah always refer to this but I'm going to show you how to use it anyway so let's go back and open our project in visual studio code. To do this I can do code with . and it should open visual studio code for me but this basically just opens the project folder here on the left side but if you want to use another editor that's not a problem just open your editor go to file and then open folder and you can continue from here. Now if you open the package.json file you should have the dependency .EMV installed and as of currently we are on 10.0.0 of course if you're watching this video in future the version might change and so on. Now what I'm going to do is let's start by creating a very simple hello world application literally we're gonna console log a hello world and we're gonna get the hello world from the environment variable so to do this we need to create two files the first file is the .EMV file this is where you store your database connections API endpoints application keys paths port numbers and so on and what I'm gonna do now is I'm gonna show you a very very simple example so for example we can have message and this is the environment variable and then we need to give it a value a string so this message will be equals a low world like so and this is our first variable now usually by convention all the variables need to be capitalized and you can separate them by doing underscore for example a common thing that you might see is something like database name like so and then equals and then you put the database name like a host and so on let me remove this now and let's see how we can grab this message and display in our application now let's create a new file called app.js and you might have already guessed that this is going to be our application to be able to display the environmental variables we need to require .EMV to do this it's actually fairly simple all we need to do is require somewhere at the top of our application usually and do require .EMV in single quotes or double quotes I guess and then let's do config which is the method that we're going to use and then close this is the very basic usage of .EMV and basically this is going to look for the .EMV file in the main directory and use it but what if you wanted to have the .EMV file in another location well basically inside config you can pass different parameters so for example if you have different location different path you will have to put in curly brackets path and then the path will be in single quotes like so and then you do custom path and then your file name which is .EMV like so this is how you do the path and I'm going to remove this as we won't be using custom path and the other thing that I wanted to show you is if you wanted to change the encoding which is set to utf8 as default if you want to change it to latin what you can do is do in curly brackets you can do encoding like so and then you can do and then you can do latin one like so and this should work right I'm not going to be using this as well so I'm going to get rid of it and leave it as it is and now let me show you and how we can bring this variable here in our application so to do this all we have to do is console log something console.log and to console log the variable all we have to do is use the process .EMV and then . and then we put the variable name which is message in this case so message like so and close this and if we were to run this application now I'm going to go to the terminal new terminal and I'm gonna run it here so we can do node and then we need to run the app.js file press enter and as you can see we're getting hello world which comes from the .EMV file if I was to change this to hello world one save it and if we rerun this application you'll see that we're getting hello world one which means that this is working okay this is a very very basic usage but to be fair that's almost all you need to know and the last thing that I wanted to show you is kind of like a real life example and this example comes from the official Node.js website where they show you how to set up an HTTP server so let me show you what I mean so inside here this is like a super simple basic HTTP server so I'm going to copy this to save you a little bit of time you can post the video and copy and paste if you wish so this is a real life example obviously a very basic one I'm gonna remove this and what I'm gonna do is let's paste all of this in here I'm going to we can maybe put the HTTP at the top it doesn't to be honest it doesn't matter too much and as you can see okay let's start from the top we require the HTTP and inside here is what I wanted to show you so at the moment we kind of we have the host name hardcoded in here and we have the port name hardcoded in here as well and then we create a very basic server which will display hello world as plain text I will show you in a second we might as well run it and see what we get and then we're basically listening on this port number here and then the host name and then we are displaying a console log of running server at HTTP 127.0.0.1 with the port number of 3000 if I run this you will see okay let me save this if I run this you will see that we get server running at 127.0.1 at 3000 so if I open this you should see hello world which is good but I wanted to show you how we can use the variable how we can set these as environmental variable names instead of having them hardcoded in here and you probably already know how to do this by now so what I'm going to do is jump to the environment file let me grab this first I'm going to jump back in and I'm going to do host name equals 127.0.0.1 and I'm gonna do port and I think the port was set to 3000 so let me save this let's go back and instead of having them here hardcoded let's do the process.env and then host name like so and I can copy this and put process.env port like so and technically nothing else should change we should be able to get the host name and the port name save this control and see your command and see to exit this and if I rerun by the node app.js you should see that nothing has changed everything is working it's all working here on the port 3000 maybe we should change the port number to 5000 for example save this close this rerun app that now if I refresh this this won't work as you can see it's spinning and if I go to 5000 as you can see it's working so this is pretty much the basics of how you can use .env it's a pretty powerful way of loading environmental variables this is going to be pretty much everything from this tutorial I hope that you enjoyed it I hope that you learned something new and consider subscribing to my channel I upload videos just like this every single week and thank you very much for watching and I will see you in the next one