3 Working with Node.js and npm

 

This chapter covers

  • How Node.js works
  • Managing dependencies with npm
  • Using JavaScript as a system-level scripting language

In 2009, Ryan Dahl announced Node.js. His goal had been to create a system that would operate more efficiently than the venerable Apache HTTP server thanks to non-blocking IO, a strategy that would keep the CPU from sitting idle while waiting on the disk or the network. To do that, he needed a language that made it straightforward to write asynchronous server logic. JavaScript, he realized, was the perfect fit.

Today, Node.js has evolved beyond its humble origins and become a major platform, not just for servers but for applications of all kinds. The secret to its success is the JavaScript language itself, which has proven far more versatile than its humble origins in the browser would have suggested.

In this chapter, you’ll learn how to create and manage JavaScript projects with Node.js and its popular package manager, npm. Many of the topics covered in this chapter can be applied to all kinds of JavaScript projects, even those aimed at the browser. Regardless of the target runtime, Node.js and npm can be used for managing dependencies, running tests, and building code for production.

3.1 The Node.js runtime

3.1.1 Running the Node.js REPL

3.1.2 Using Node.js APIs

3.2 Managing Node.js dependencies

3.2.1 Installing dependencies with npm

3.2.2 Understanding node_modules

3.2.3 Syncing and updating dependencies

3.2.4 Forking and patching dependencies

3.3 Scripting with Node.js

3.3.1 Defining scripts in package.json

3.3.2 Writing executable scripts with Node.js

3.3.3 Running package executables with npx

3.4 Summary