All right.
And our first task is going to be setting up ES6 modules on the server.
So essentially, remember how we worked in the client.
Every time we needed something, we just went with import syntax.
For the longest time, Node was using a little bit different approach.
It's called Commonjs.
Again, the idea is exactly the same.
Essentially, you're not writing your entire code in one file.
You are creating folders and files and all that, and then you just import whatever you need.
And technically you can still use commonjs.
Again, the idea is going to be exactly the same, but since we're using ES6 modules on the front end,
it kind of makes sense that.
We do the same thing on the back end.
Now it's not happening right out of the gate.
First, we need to navigate to Package.json in the root.
Again, I'm going to be annoying and I'm just going to keep repeating that it's in the root and effectively
in here we want to go with Json syntax.
So essentially we need to go with double quotation marks.
Yes, that's important.
And notice right away we get the suggestion and we want to go with module.
Let's save it and let's create a test file.
You know what?
Let's create two of them.
Let's call this test, um, named.
JS and also create test and default.
JS Okay, good.
Then in the test named.
Let's create a value.
And if we want to export something, we simply add export in front of it.
And I fully understand that this might be a little bit redundant since we did use the ES6 modules already
in the client, But I just want to make sure that we all are on the same page.
So for example, over here I could go with value and set it equal to 42 and I'm sorry, I'm setting
this up in a default.
This should be named My bad.
And then let's go over here and let's say the test default one some value and I'm going to set it equal
to whatever some value.
Basically I want to showcase that we have a few approaches.
One is the named one where we have the variable and we just add over here the export in front of it.
And when it comes to default, just like on the front end, we get one default export per file.
So let's say if I want to set this one up as a default, I go with export and then default and.
Some value.
So backing the server.
If I want to access the named one we need to go import then value since names need to match over here
in this case from and then we navigate to a test named.
And when it comes to a default one, well we can change it.
So in here I can call this a random value.
And again, we're looking from and we want to get it from the test default.
So if we log both of these values over here, one is going to be value and one is going to be a random
value.
It's not going to be probably surprise when I run node server that in the.
Console.
I'll see the values.
Now, here's the major, major, major, major.
Gotcha.
We need to remember when we work with ES6 modules on a node, we do need to add this JS extension.
So remember when we were working in the client, I mean, we were not adding it, right?
We just went with the file name and we're good to go.
When you're working in Node and you use ES6 modules, yes, we need to add the JS extension, so we
need to navigate here.
All of this is correct.
We just need to add these.
JS Again, let's navigate back.
To our.
Integrated terminal.
If you want to go back, just press arrow up.
Effectively, you can just look for the latest commands and check it out.
Now we have 42 and some value.
Now there are some variations to this and I'll try to cover them during the course.
Basically the idea again is the same.
We just can have a little bit different syntax, but the main idea is not going to change.
So we have one default per file which you can use.
And of course then when you import, you can name this however you please.
And then with the named ones, yes, you do need to use the same value.
And in order to get started with modules, with ES6 modules and Node, we need to type type module in
package.json and whenever we want to use our modules, we need to remember to use the JS extension,
which probably I'll forget as I'm working on a project.
Just a fair warning since it's very easy to get used to the client, one where you don't have to do
that, and then when you start working on a node you're like, Oh wow, I need to keep typing those
JS extensions.
Hopefully that is clear.
And now we can move on to the next topic.