Chapter 2. Globals: Node’s environment

 

This chapter covers

  • Using modules
  • What you can do without requiring a single module
  • The process and console objects
  • Timers

Global objects are available in all modules. They’re universal. Whether you’re writing network programs, command-line scripts, or web applications, your program will have access to these objects. That means you can always depend on features like console.log and __dirname—both are explained in detail in this chapter.

The goal of this chapter is to introduce Node’s global objects and methods to help you learn what functionality is available to all Node processes. This will help you better understand Node and its relationship to the operating system, and how it compares with other JavaScript environments like browsers.

Node provides some important functionality out of the box, even without loading any modules. In addition to the features provided by the ECMAScript language, Node has several host objects—objects supplied by Node to help programs to execute.

A key global object is process, which is used to communicate with the operating system. Unix programmers will be familiar with standard I/O streams, and these are accessible through the process object using Node’s streaming API.

2.1. Modules

2.2. Standard I/O and the console object

2.3. Operating system and command-line integration

2.4. Delaying execution with timers

2.5. Summary