Chapter 3. Basics of Node Modules and Node Package Manager (npm)
Throughout the book you’ll have a chance to work with several Node modules, most of them core modules. These modules are included with the Node installation, and incorporated into the application using the global require
statement.
In this chapter, we’ll explore the concept of the Node module more closely, look at the require
statement in detail, and introduce you to npm, the Node package manager. We’ll also go shopping for some non-core modules that many people believe are essential for any Node application development.
An Overview of the Node Module System
Node’s basic implementation is kept as streamlined as possible. Rather than incorporate every possible component of use directly into Node, developers offer additional functionality via modules.
Node’s module system is patterned after the CommonJS module system, a way of creating modules so that they’re interoperable. The core of the system is a contract that developers adhere to in order to ensure that their modules play well with others.
Among the CommonJS module system requirements implemented with Node are:
-
Support is included for a
require
function that takes the module identifier and returns the exported API. -
The module name is a string of characters, and may include forward slashes (for identification of paths).
-
The module must explicitly export that which is to be exposed outside the module.
-
Variables are private to the module.
Some Node functionality ...
Get Learning Node, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.