O'Reilly logo

Learning Node, 2nd Edition by Shelley Powers

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required