- [Instructor] In the last chapter,
we took a look at the objects and functions
that are globally available to you.
One of those functions that is globally available to you,
if the require function.
And we used it in the last chapter to load the path module.
Let's do that again.
Let's go ahead into our exercise files
and create a new file called core.js.
And within this core.js,
I'm going to create a variable called path
and require a path module.
The require function is what we use to load modules.
Now many of the modules that we load and work
with are hosted on NPM and delivered by the community.
We'll talk more about NPM and those modules later
in the next course.
For now, what we're going to focus on in this chapter
and the next few chapters are those node modules
that you don't have to install with NPM.
These are the modules that are installed locally
with your installation of Node.js
and we refer to them as the core modules.
The path module is a core module
because it came with Node.js.
We can use it out of the box.
And the path module helps us do a lot of wonderful things.
Like, for instance, let's say
within our exercise file folder,
we had a folder called WWW.
And within our WWW folder,
we had another folder called files.
And within the files folder,
we had another folder called uploads.
And if I wanted to create a path to
that from my code.js file,
I could use the path module to help me out with that task.
So I'll go ahead and create a const dirUploads
and this will point to the uploads directory
using path.join and __dirname.
That will give us the full path
to the current directory, our start folder.
So if I wanted to reference the WWW, files,
uploads directory, I can just supply those as arguments
to the path.join function.
And we can go ahead and log the dirUploads
to the console so we can take a look at what
that will look like.
So I'm going to go ahead and open up my terminal.
And I'm going to go ahead and also close the files.
And from here, let's go ahead and run node core.
And you can see the full path to the folder
that we're in, chapter four, chapter four one start,
and I also reference the WWW, files, uploads directory
and we join that all together using the path.join function.
There are tons of helpful modules
that came with your installation of Node.js.
So I'm going to load another core module,
the utilities module, into a variable called util.
And using the require function,
I'll load a modules named util.
And this module has a lot of helper functions
that we can use but specifically they have a log
that's a little bit more powerful than the console log.
So I can say util.log and we can take our path
and use the base name function, we'll send it the __filename
and that will give us the current name of the file.
But we're going to see this in a utility log.
So I'll go another utility log,
do a little up-carat and say the name of the current file.
So let me go ahead and save this.
And we can pop open that terminal and run our model again.
Node core.
And you can see that the utilities model logs our strings
to console like the console log would
but with the utilities module, we also get the date.
So every log has a date and time stamp associated with it,
which might make logging a little bit more official.
The primary difference between the utilities module
and the console log is that we had
to require the utilities modules.
So here on line two we had to load the module
before we can use it and console log
we can use out of the box.
There are tons of helpful modules.
Let's take a look at one more.
I'm going to go ahead and load up a v8 module.
And for the v8 module I'll just do a util.log,
v8.GetHeapStatistics.
So the GetHeapStatistics is a function
that's available to us on the v8 module.
So I'm going to save this and we go ahead and run it,
we'll go ahead and take a look at the memory usage
and our memory statistics.
So we'll do node core and, as you can see,
this shows us the total heap size of our menu,
the physical size, available size, the heap size limit,
and so on and so forth.
So there are dozens of modules that we can use
to help us build applications within Node.js.
And let me show you another little trick
that you might come across in Node.js files.
All of these functions that we want
to use can actually be destructured out of their modules.
So, for instance, we're using util.log.
Well if I came up here and,
instead of naming the full variable util,
I can just pluck out the log function
by destructuring only the variable that I need.
The same is true for v8.
If I only wanted to use the GetHeapStatistics function
I could do so by destructuring it out of
that require statement.
So then it might make my code look a little bit better.
There are arguments for or against this
because at the same time, we might not necessarily know
where the log function is coming from
or where the GetHeapStatistics function is coming from,
unless we look at all of our require statements.
Let's go ahead and just give this a quick test.
Node core.
And we still see the same utility log
and our heap statistics.
So there are more modules that are useful for us
to require out of the box.
And for the next couple chapters,
we're going to focus on these core modules in particular.