Modularity is a software design technique where one program has distinct parts, each providing a single piece of the overall functionality. These separate modules come together to build a cohesive whole. Modularity is essential for creating scalable programs which incorporate libraries and frameworks and separate the program’s concerns into manageable chunks. Essentially, a module is a collection of code located in a file. Instead of having an entire program located in a single file, code is organized into separate files based on the concerns they address. These files can then be included in other files by using the
To save developers from reinventing the wheel each time, Node.js has several built-in modules to perform common tasks efficiently. These are known as the core modules. The core modules are defined within Node.js’s source code and are located in the lib/ folder. Core modules can be required by passing a string with the name of the module into the
// Require in the 'events' core module: const events = require('events');
The example above shows how the
events module is required into a file and stored in an
events variable. Understanding the specifics of this module isn’t necessary at this point, but the
events module is a Node.js core module that provides key functions for working with, well… events. You’ll learn more about it in a later lesson.
Some core modules are actually used inside other core modules. For instance, the
util module can be used in the
console module to format messages. We’ll cover these two modules in this lesson, as well as two other commonly used core modules:
Node.js has several core modules, far too many to cover in this lesson. We’ll learn how to get the full list and then dive deeper into the aforementioned modules throughout the next few exercises.
Let’s start by listing all of the core modules built into Node.js. We can do this from the Node REPL. To enter the Node REPL, type
node in the terminal and press enter or return.
Once in the REPL, a complete list of core modules can be accessed by typing the command:
As you can see, there are many modules already built into Node.js and ready to be utilized! In the next few exercises, we’ll explore some of the more useful ones in further detail.