In this video, we are going to learn Node.js modules.
So what is a Node.js module?
We can consider Node.js modules to be the same as JavaScript libraries.
It is a set of functions that we want to include our applications.
So let's make this logger file as a Node.js module.
As you remember that in the last video we have created the local JS and local function, but now we
can create these logger operation as a Node.js module.
In order to do that, we are going to change these method with the method which they should be the exports.
You would.
Yes, exports cost.
And I would like to change the name of the method which is the right logo.
And now we don't need to invoke this function inside of the file.
It should be export, not exports.
So as you can see that this is the method body that we can use the export keyword and it is basically
exporting these functions to the other files.
That means this becomes a Node.js modules.
So now how we can use these Node.js module into other files in order to do that, please create an index
case and into the index case.
I'm going to write a function, for example, main function.
And in the main function, we can basically make our log operation, which is the main function.
Starts.
And after that, we can.
Bite the right look.
So when I write the right look, operation method, as you can see that it is going to add inputs to
a statement from the logger file.
So if you click this IntelliSense keyword, you can see that it is basically import logger class and
logger JavaScript file and import write log method.
So by this way, we can use this method into our main function from the index JS file.
So in order to do that, I'm going to pass two parameters.
I would like to paste two parameters.
So instead of we are getting the arguments, we can change these local function to get the name and
surname as a parameter.
So let's make a little bit change.
So name and surname is created as a parameter for this function.
So in order to use this function, I'm going to set a these name and surname from the parameters.
Now we can remove this line of code.
Okay.
So as you can see that this very basic Node.js model.
It is getting the name and surname as a parameter and it will make the log message.
But we expose this right lock method into the indexes and to do so in order to use this function.
As you can see that we should provide the name and surname.
So I'm going to give my name and surname basing this function.
And after that, we will call these mean method to invoke all these operations.
Okay.
Now I'm going to run the node command node.
Next JS file and see what's happened.
So when we run this not comment, we get exception, which is the cannot use import statement outside
a module.
So what is exception is throwing?
So if you scroll up you can see the warning to load an is module.
We should set the type module in package JSON file.
So this is related with the module explanation in the Node.js function that I'm going to explain in
the upcoming videos.
But I can say that Node.js has two types of modules.
One is the common JS.
Another is the script name version.
So we will use a script modules.
So to activate it, we should set the type module in the package JSON file.
So let me add the package JSON file for now and I will explain in the outcome could use.
But you can add the package JSON file after that.
We can set the type and you can see that there is two types of Node.js modules, common JS and module.
We are going to use module in order to activate the latest script codes.
So let's save this file and this time in the local file can be exported method with using the export
keyword and after that index file can be used this local operation with importing the logger file.
Okay, now after that, let's run this command again.
Note index is hit enter.
So this time again we got another exception.
So this is also important exception.
So as you can see the message did you mean to import logs?
So sometimes in some cases it is explicitly not that the full folder name.
So that means when we are importing this logger operation, we should provide the logger dot js file.
So this is important.
Please save this file import statement because when we are adding these import statement with using
the values to do code IntelliSense, it doesn't include this JavaScript file and we should add the JavaScript
file explicitly.
So after that, after make this change and save these indexes file.
If you run the note indexes comma hit enter.
We can get the success response in our case.
So as you can see that the first log is come from the main function starts and the second log is comes
from the our right log operation, which is exposing from logs.
We can right click go to definition and see the log operation, which is your name and surname is Mammoth
Square.
Okay.
As you can see that we understand how Node.js model works and how we can use modules in our Node.js
application.
If we examine the Node.js lambda function, for example, I have put the calculator lambda function
code in here.
You can see that we have all of the exposed 100 method in a common module version.
You can see that this time it is using exports and it is exporting 100 method.
So if you look at the lambda function, this is the runtime Node.js runtime is running the handle method
when invoking any location comes to lambda function.
So we are exposing these 100 method with using these exports 100 method body.
But we can change these export 100 which we must equip Node.js module with adding package JSON we will
do in the upcoming videos.
But if we examine the our calculator JavaScript file of the lambda function, it is basically exports
the one handed method after that one event in the lambda function.
This will trigger to handle method with incoming event JSON object.
So we have learned the basics of lambda node.js functions and the modules.
As you can see that we have create a Node.js module and use it this module into our index file.