Hello everyone.
In this video we're going to talk about NVM.
NVM is Node Version Manager and we're going to use it for our Firebase project.
And we're going to talk about why it's important for us to install NVM.
Before we dive into NVM,
let's discuss what it means to run a node application on Firebase Functions.
Firebase Functions is a serverless backend
platform that allows you to write and deploy serverside code without
worrying about server management or infrastructure.
And when you create a new
Firebase Functions project, the platform automatically sets up
a Node JS runtime environment for you which includes a specific version of node.
And as you see here,
the Node version that is currently used for Firebase functions is 16.
Therefore, it's important that our local development environment matches the same
version of Node that Firebase function uses.
And this is where NVM comes in.
NVM, or Node Version Manager is a command
line tool that allows you to install and manage multiple versions of node
js on your local machine.
This is important because different projects may require different versions
of Node, and by using NVM, you can easily switch between different
versions of Node without having to install and uninstall them.
So for example, if I want to check out
my current version of Node, I can type in node v and you can see
that I'm on 19 font 5.0, which is different from 16.
And this is where we could use
Node Version Manager to make sure that we also could install the 16th version.
So let's get started with installing NVM for that
what you might need to do is just go
to a new browser, type in NVM and you can just go to the first link here.
That is the node version Manager.
And here you're going to see the installation command.
So just copy this and paste it here.
I already have it installed so you might
see a different output, but just wait for it to complete.
And then what you could do to install
the 16th version is follow the directions given here.
So let's say NVM install 16.
I already have it installed so you might
see a different output but wait for this command to complete.
And now if you want to switch to use 16 by default, the command that you would
need to use for that would be NVM alias default 16.
And now my default version is going to be
16.20, and if I close this terminal reopen a new one and I type in Node v,
I'm going to see that my current version is 16.20, which is great for our Firebase
project because they're going to be using version 16 of node as well.
Great.
So now that we have that set up complete, what we can do is actually start
transferring our server JS to use Firebase Functions.
So inside index JS file,
which is going to define what our server will allow us to use as functions.
You see that we're requiring Firebase
functions and we have an example here of how to create a new one.
But we're not going to go with this example.
We're actually going to create our own.
And I don't want to be using require because this is an old way
of defining what is imported for us and I just want to use imports.
And currently Firebase functions doesn't really allow it.
So we're going to have to change up our
package JSON file to allow for latest ES6 syntax.
And to do that, first of all we're going to go to package JSON.
We're going to say that we want a modular setup as we did on the last one.
And we're also going to include some other dependencies.
So we are using body parser and Express.
Therefore we're going to have to add them
here and you can just copy it from our setup
here and place it in the dependencies section.
And we have some dev dependencies such as
Babel which is taking care of some of the things for use.
So let's put them here as well and save
this and we can get rid of package JSON and package-lock jSON from here we don't
need them anymore, so we can just delete them.
Great, because all of our setup is going
to be in this functions folder from now on.
And then what we need to do is make sure
that our index js file now imports these functions instead.
So we can just say import everything
as functions from Firebase functions and then we can
comment this line out because we're not going to be using it anymore.
We can just go to the root of our project
and then change our directory to functions and we can just type in NPM install.
So let's wait for this to complete and our package lock JSON file will be
updated and these will be installed for us inside our node module.
So everything should be all set. Great.
So now that we can use import as functions from Firebase functions,
we can just transfer whole our code inside index JS file.
Before we do that, we're going to have to transfer our
babelrc file actually inside our functions.
So let's do that.
And here we're going to have to say that
our targets changed because we're going to have to make sure that our Babel preset
environment is dependent on the node 16 when installing.
So let's make sure that we have another array defined here.
And then inside here we're going to have
to create a new object that makes sure that our target is using node version 16.
So let's just put this object here.
Let's make sure that this is closed as well.
Great. So now that we have updated our babelrc
file using node 16 for installation and we have updated our package JSON file as
well, all we need to do is make sure that we reinstall everything again just
to make sure that everything's set up for us correctly.
And then we can just copy all of this code
except for listening to some port, and then we can just put it inside here.
And then we can use this example that they provided to set up a new function.
So let's do this and let's call our function app.
And then what we want to do is actually use functions Http on request.
And instead of handling just
one API call using app, we're just going to put our app that we
defined inside here as a handler of what should happen on an API request.
Great, so let's save this right now.
And then what we're going to have to do is the following.
We can delete the server JS file because we don't need it anymore,
and everything's all set inside our project.
So now we're going to have to actually deploy our changes to Firebase functions.
Great, so now that we're ready for all of this, all we have to do is
run NPM run deploy, and it will connect to this code given
here that will automatically deploy our index js file to the Firebase functions.
So let's run npm run deploy and we're going to see this output.
And we're getting an error here which says
that our project should be on blaze pay as you go plan to complete this command.
So let's do this.
I'm going to click on this link and it should redirect me to our project.
So here it is.
So I'm just going to click on Modify my plan and I'm going to click
on Selecting this plan, and I'm just going to continue
and I'm going to skip this step and I'm going to say purchase.
Great. So now that I'm on the Blaze plan,
I should be able to continue with my deployment.
So I'm just going to type in npm run deploy again.
Okay, so because we're using ES6
module, actually we can't use exports here as given in our example.
So let's modify our code
and say export const app.
But we already have defined app,
so I'm just going to call it Hello World and then I'm going to say functions https.
Actually, I'm just going to copy this
and I'm going to get rid of this line.
And now I'm going to run the deployment again.
Great, so everything want successfully and it's being uploaded.
So let's wait for this to complete.
Great, so our deploy is complete and if we
want to check out our function URL, we can just click on this link here.
So I'm going to click here and we're going to see our hello world response because we
have this get function here that responds with hello world on that link.
So if we were to, for example,
use the user get function, we're going to see the function that we
set up for our API endpoint responding with your getting a user data back.
So we can just copy
this URL, open our postman and paste it here and then send a request.
And we're going to see you're getting a user data back here as well.
So if we want to make a post request
that we get up, for example, let's go to our post here,
we see that we have an endpoint for the user and let's send in some parameters.
So select body here, then Raw
and let's set this to JSON and let's say that username is Nata,
first name is Nata and last name is Vacheishvili.
You can send whatever other requests you
want, but we are using in our post request the first name parameter that we send in.
So let's send this post request and you're going to see a message.
We created a user with first name of Nata.
Well, we don't have a database set up, so we actually didn't create anything
in our database, but this is the response that we set up here.
Therefore, we can verify that our firebase
functions are working as they were working on our local host.
That's it for this video.
I hope you found this useful.
Thanks so much for watching.
And in the next section, we're actually going to use firebase
functions to set up stripe payment system for our donation application.
See you in the next video.