- In the last lesson,
we created and ran our first Node.js file.
We also learned about the global
which contains the functionality
that is available to us within a Node.js file,
without having to include
or require any other modules.
In this lesson we're going to continue
to explore this global object.
I'm going to come over here into the file folder,
and add a new file called global.js.
And another thing that's available to us globally,
is the name of the current file
as well as the full path to the directory
that we're currently using.
So console.log(__dirname);
is going to give us our directory name,
and console.log(__filename);
is going to give us the full name and path
to the current file.
So I'm going to go ahead and open up the terminal,
and run this file by typing
node global.
And when we run this
we can see logged to the console
the full path to the directory that we are using,
as well as the full path to the directory
including the filename of the current file
that we've run,
global.js.
Node.js also comes with some tools
that will allow us to edit
and manipulate filepaths,
but we have to actually load these tools
because they come in a separate module.
And that's okay,
because also available globally to us
is the CommonJS module pattern.
This pattern is how we import
other code into our files.
There's a function called require(),
and then there's an object called Exports later.
But for now, let's take a look
at how we can import other modules.
So the require() function
can be used to load external modules,
which are other JavaScript files containing code.
We can either load modules that have shipped
with our installation of Node.js,
or modules that we install with npm,
or other modules that we create,
just other Node.js files
in our directory structure.
Right now let's look at the path module.
So the path module is one of those modules
that ships with Node.js,
and it gives us tools that we can use
to help us work with path strings.
So I'm going to create a variable
constant called path,
and we will load the path module
into that variable.
So now that we have the
path module loaded in this file,
we can use the basename() function
to pluck the filename only,
from the global __filename variable.
So I'll go ahead and log()
with a template string.
I'm going to go ahead and use backticks.
Those aren't single quotes,
that's the backtick character.
The file name is,
and I can go ahead and add a variable
to this template string.
We'll use the path module,
and on the path module
there's a function called basename(),
and we can send the __filename value
to the basename() function,
and it should pluck the filename.
So let's go ahead
and come down to the terminal here,
and run our file by typing
node global.
And we see that
using the path.basename() function,
has allowed us to pluck the filename
from the full path.
So every Node.js file that we create
is referred to as a module.
It contains its own code,
and when we want to load other modules
we must use the require() function,
that is available to us
on the global object.