Before we get started with x rays, we need to understand what is import and export.
Where Node.js uses the common JS module system and the built in required function is a easiest way to
include modules that exist in a separate files.
So this simply means that with import we can use a module or a function from a different file, or we
can use import to use third party modules or packages from NPM and use it inside our application.
So import simply means get right.
We are getting a function to be used in application and export simply means that exposing the custom
function that we have created so that it would be available in all parts of our application.
So let's demo that to better understand what is import and export.
But before we get into that, we have two ways of doing the import and export.
Like I say, by default, Node.js uses what is called common JS module system and we have new way of
doing it that is using the easiest way.
But for the meantime, let's take to the Common JS Center because this is what I'm going to see in most
of the tutorials and blogs and other courses.
So as you move on, I'll be showing you how we can use the module way of importing and exporting files.
So let's get started.
So as usual, we need to find a way to keep the files that we are going to play around with it.
So let me create a folder and I'll call this one as import dash export.
The name is up to you, then let me see the end to import and my exports.
So now let me open using code period.
To this point, you know how you do it.
That's why I'm moving files over here.
Now here comes the clue.
Let's say that we have two files, so let's go ahead and create those files.
So let me use the terminal to create those files.
So here we go touch and then I'm going to create app dot js file.
And I also want to create.
Let's say man starts jazz fire.
And lastly, I'm going to create what is called Utils Dot G's file.
Now I have three files inside my project as that.
All right.
So for the meantime, let's remove the app to G's file.
Well, let's use these two files to explain import and export.
Before we get into that, let's dive in more to talk about the required function.
We have been using the required function a lot.
So what does it mean?
So inside the main here, let's try to console.log.
The required remember required is a function.
So go ahead and then console.log the required and let's see what it means.
So back to my terminal here.
Let's run the file main.
No.
And then men and I see I have something being displayed, which is an error.
It tells me that an invalid, a given type, which means this is a function, but when calling it,
we didn't provide the actual argument to the function and it must be a string.
So what is the required function?
Well, the required function basically reads JavaScript file, execute it and then returns the export
object.
So for this one, we can pass in any path or a module we want to use.
The take note here is that if we are requiring a lookup file, we need to use a path dot and voice slash
and locate the file.
But if you are using a module from NPM or node, we don't do like period for Slash and the name of the
module as we did for example, path, which is what a call module.
So we don't specify the dot invoice slash, but for lookup files, this is how we do it.
All right, now that is about the required function.
So now let me remove that.
Let's come back to import and export.
So let's say that on the utils is going to have a code that is going to be used in any part of our application.
But for the meantime, let's see how the required function works.
So inside the utils file here, let's just console.log a simple text and say that.
Welcome.
Now when I run the application or the file, it will see that we're not going to see anything being
displayed because these two files are independent.
But as soon as we make this required and then the path to the file period and then first light, then
I have the file because is JavaScript need to bring JS but JavaScript you don't need to bring the extension,
we can ignore it as that.
So as soon as I run the file, let's see.
Indeed I get something on the terminal.
Meaning worker.
Meaning that we're able to use the required function and then use this file inside the main file.
As that remember required is a function so we can assign a variable to this function call.
So let me call this one as file one my first file.
So now let's go ahead and then console.log the file one and let's see what we have.
Now back to my terminal here and indeed I see two things.
One is the string will come and an object which is my module export.
Now the takeaway here is that the required required to use a file in a different file.
So now back to business about module dot export.
Now we have what is called module dot export.
So let me write down here module.
Dart expert.
Perfect.
So what is it?
Perfect.
The module that exports is an object or is a special or crucial or essential object that is included
in every JavaScript file in your application.
And the module is a variable representing the current module, with export being an object that will
be exposed as a module.
So anything assigned to the module, the export will be exposed as a module.
So what I mean here is that before I explain, let's console.log the module.
That way you better understand what I'm trying to refer to.
Now here we go.
Back to my terminal.
Here, let me clear it.
And now I see something as an empty object.
But if I remove the export now let me run again.
And let's see.
I see an object with export being property.
And because it's an object, we can add any property into the object.
So now back to business.
So let's say that we have a string or a function inside this file.
How can I use it here?
So for the meantime, let's try to export a string, call it as literal as that.
So here we go.
So for string, let's just send a simple text, say, welcome to the jungle.
So here, let's make use of module dart export.
And now you're going to add our own property called Message and Equal to work.
Welcome.
Okay.
So let's save it.
Now, inside, man, how can we use this, remember?
Because we have made use of required.
And then we assign to a variable called file one.
Now, if you console.log the file one, let's see what you are going to get.
Now back to the terminal here.
And now we see that I have two things.
One is a string, which is this one work.
The next is objects with property, which is welcome to the jungle, meaning this one, because I've
added a property code message on the export.
So now I have it as that.
So if I want to have access to this string, I'll make use of file one dot m sg.
Meaning I'm using this particular property.
So if I run the file again, now I see.
Welcome to the jungle.
So that is how we can export.
What, literals?
So now let me comment this one.
So how can we also export objects?
So now come here as export.
Objects.
We can do it in this way.
Module dot export.
Now is equal to object with some property names.
So for this, let's say we have a product.
Name.
Let's give it a value.
Say Mark book.
Pro.
And then let's say the prize for this is going to be $1,000 and then description is going to be as a
laptop.
Okay.
Now we have exported this object.
How can we use it again?
Because we have required this function.
We have all the properties and methods on the variable we assign to this function call.
So now if I save this one, let me clear the terminal here and now check it out.
And you see that I have the object here so we can make this of dot the property names.
You can see I have description, price and product name.
So here we go.
When I run it, you see that I have laptop.
That's how we can export.
What objects?
So what about how can we also export functions?
And this is what we want.
How can we export function?
So down here let me comment this one and now we have a beautiful one as what export function?
The function going to be a simple one just to great with some static text.
So let's say great, you're going to be like this and let's just console.log static texts and say.
How?
How are you?
Great.
So now how can we expose this function unless we export from this file?
So here we go.
Module dot export.
And that is equal to my function as that.
Don't call it ize this, export it as an object.
So in this way we have grid as a property on the object.
So now inside Main, now we have the method of function on the file that we got here.
So again, if I run the file nine, we see that I have a function with grid, so it means that this
one represents a function now.
Right.
So I can quickly go ahead and then run the function.
So going to be five one as a function.
So now let me move this one.
Now back to the file and now you see, how are you?
The function has been called.
That is how we can.
But I think the naming here is not ideal how it's just used to explain to you.
But let's say that we are requiring a function.
Definitely.
The name must be descriptive.
We can say grid.
As that so that we know that is a function for grading.
So if I save it, I'm going to be the same as before.
So it is how we can export function.
What about if you have multiple functions here?
Let's say the first one is great.
The next function is going to be say, AD, and this time around is going to take two, argument A and
B and then as we go, then here let's console.log A plus B.
As that.
Now we have two functions here.
Can we export in this way?
Great.
And comma and let's say add.
No it no work unless we export them as an object and we can do it in this way with a property and the
value.
So here the property can be anything.
Let's say my greeting.
Now is equal to the great function above the.
And then next I'm going to be my ADD function is equal to the the function above which is add as that.
So now inside this one, instead of doing this way because I'm exporting multiple functions here, we
need to structure the properties or the functions or the methods for the meantime, let's ignore this
and nonetheless console.log, let's say file for the meantime.
Okay.
And now if you try to console.log, it's let's see what we have now we have these properties and methods.
The first one is dealing with a value, a function, and the next I'm going to be my ADD and a function.
I think I made some type mistake here is going to be my ADD.
So let's remove this one and let's use the name of the function as great.
So here are going to be grids and then grid and next I'm going to be ADD which represents the ADD.
But for ESX, if the property and the value are the same, you can choose the right one and we can remove
this one also.
Now, perfect.
So now back to the file.
Let's go ahead and run it.
So let me click my terminal here and now indeed is the same.
So now if we want to call the grid here, I'm going to make use of file dot grids, then we'll call
it as a function as that.
Do you get it?
Yes.
So save it now let's go ahead and console.log it.
And you see I have.
How are you.
What about the add.
Let's say add the you pass in two and three and let's see.
Yes, I have five.
So instead of doing this way I think it's not clean.
But instead, like I said, we can structure the properties or the meters from this.
So here we have grids and then the add as that.
So this one, if I make use of grid now if I save the file and run the code again.
I see.
How are you?
The same thing.
I can call my add function and pass in three and six.
And if I save it, let me remove this console.log here.
All right, let me comment it.
So now back to the file.
Let me clear the console.
And now indeed I have Ryu and then nine meaning that these two methods has been called and this is a
pattern we are going to use a lot as you move on in this course.