In the last chapter, we took a look at the objects and functions that are globally available to us. One of those functions that is globally available to you is the required function. In the last chapter, we used the required function to load the path module. Let’s do it again. Let’s go ahead and navigate to our files.
The require function is what we use to load modules. Many of the modules that we use are hosted the NPM registry and need to be installed first. We’ll take a look at those modules later on in the course. For this chapter, and the next few chapters, we’re going to focus on those modules that you do not have to install with NPM.
These modules were installed locally with your installation of node JS. We refer to these modules as core modules. And path is one of the core modules available to you with node JS. In the last lesson, we used the path module to pluck the base file name from a full path.
We could do that again. I could log the path.base name. And that is a method that we can send a full file path with name to. And we have that available to us on underscore, underscore, file name. And it will pluck out the base file from that full path. Which, in this case, will be core JS. We can also use the path module to create path strings. The path dot join function can be used to join strings together in a path. Ver dirUploads will be a new path that I make to the Uploads directory using the path.join function So, I will join this to the current directory.
And we will assume that it is in a www directory. And then under that, there is a files directory. And then under that, we will assume there is an uploads directory. So, what I have done is I’ve just created a long path string using the path join function, where I would send it each directory in the path and it will create the string for me. Let’s go ahead and log that. So, I will save this and go back to the terminal and run it. And, we can run this just by typing node core.js.
And you can see that the first path.basename plucks the core.js module from the full file path. You can also see that the path.join was used to create a very long path string, that includes the current directory, plus www/files/uploads. So, the path module has some great tools for working with paths. There are other modules that are available to you as a part of the node core. Let’s go back to our code.
We can use a utilities module. The utilities module is called util. The utilities module has several helper functions that we can use. One of which is a log. So instead of doing console logs, I can do util logs. And, the difference is we’re still going to log this information to the console. The utilitie module log function also adds a date and time stamp. So, I can go ahead and save this, and go back over to the terminal. And we can run our file again. This time I will run it without the JS extension.
There should be no difference between doing that. And here we see that Core JS was logged on the 26th of October at this exact time, and our full path to www, files, uploads was also logged with a time stamp. There are plenty of these modules that we can use. One module you can use to get the current details about memory usage. That’s the V8 module. So, I’m going to go back to my code. Since, no JS is built on top of Google Chrome’s v8 processor, we can use the v8 module to get information about memory.
So, there are a handful of modules that we can require out of the box with node js. For the next couple chapters, we’re going to focus on these modules in particular.