[MUSIC]
>> Hi, everyone. My name is Juan and I'm
a Senior Cloud Developer Advocate working at Microsoft.
In this video, we'll see how to install and
use packages in your JavaScript application.
To be able to use packages in my application,
I will have to create this filename package.json.
To do that, I can use the npm CLI by typing npm init.
Because I'm lazy, I don't want to install a few prompts,
so I will use this dash y option.
It will create for me this package.json file.
As you can see, there's a few information in there,
like name and version of my application.
But you don't see a dependency section.
That's because we didn't have at this point installed any package.
Let's just do that.
I will install a package.
I only need four developments.
So I will use the dash dash save-dev option
and we can start Prettier,
which is a cut formatter.
Now you can see that there's
this new devDependencies section
with the package I just installed in there.
To make use of that package,
I can add scripts.
I will name the script format.
Then we make use of
this Prettier package to make it reformat all my JavaScript code.
Now that I created this format script,
I can run it using npm run format,
and that will just run the command I'd just written in there.
To see it in action,
let's just see this badly formatted code I've just written.
I run this command, you can see that
now I have a pretty clean code.
As you can see here,
I'm trying to use the Express package,
but I don't have it yet in my application.
So let's install it.
To do that, I type npm install express.
Now I'll have this dependency section with Express in there.
Express is a package that I'll need to
create web server and web APIs.
The code I've written here is a simple web server that will listen
up port 3000 and will answer a ''Hello world.''
to our request. To run it,
I can use node index.js.
You'll see that my server is running.
To make it more convenient,
I can also add a new script named start here.
I can type just the same command as used in the terminal.
Now instead of typing node index.js,
I can just type npm start.
Start is a special script.
That's why I don't have to use
the full command name npm run start.
I can use this shortcut one just called npm start.
Maybe you have already noticed it,
but even if you're scared, I have
this npm scripts section in there.
You can use this section to do
exactly the same thing I just did in
the terminal and execute my script.
Now let's add a new package to my application called dotenv.
We do that by typing the npm install dotenv command.
Now you can see it in my dependencies here.
Dotenv is a package that allow you to manage
your application configuration, including the secrets.
For example, if I open my source code here,
you can see that the ports of my server is 3000,
but it's hard-coded in my code.
Meaning if I want to change it in production, I can't.
It also means that it's hard-coded and
everyone can see it if I push my code through a repository.
This is something that may be fine for configuration setting,
like here the application plots.
But if you have secrets, for example,
the credential to access your database,
you really don't want that to appear in your code.
If I do that, you can replace these hard-coded values
in your code by using environment variables.
To do that, I can replace
this hard-coded value here with process.env.PORT.
PORT will be the name of my environment variable.
But how to set this value here?
Because just like that,
it doesn't have any value.
That's where the dotenv package become useful.
To make this magic work,
just at the beginning, I will code it.
Let's require dotenv.config method.
This will do all the magic.
Adding this line here will allow me to create
a new file named.env,
hence the package name.
In this file, I can just put
all my configuration for my application.
For example, I can set the port to be this time, the value 5000.
What the package will do is,
at run time, read this file,
and just map all the settings in there to environment variables.
If I tried to run my application again,
you will see now that my server is listening on port 5000.
But wait, what I just did is
put all my configuration in a different file.
But if I just keep it like this,
it may still end up in your repository.
What you need to do now is make sure that this data
on file will never be committed into your Git repository.
To do that, I will create a new gitignore file,
and I will put in the data file,
but also this folder that you
can just see here, the node_modules folder.
This node_modules folder here is where
all the dependencies that you added
to your project will be downloaded.
That's not something you want to commit to
your Git repository. That's it.
Now we can be sure that your secrets are safe,
and that you can use different settings for
your application configuration in production.