liveBook · Manning

Chapter 3: Working with Node.js and npm

What you will learn in chapter 3 of Secrets of the JavaScript Ninja, Third Edition

The introduction explains why JavaScript suited Node.js’s original server goal and why Node.js and npm matter beyond server applications.

After this chapter you will be able to

Section by section

Introduction

The introduction explains why JavaScript suited Node.js’s original server goal and why Node.js and npm matter beyond server applications.

Key terms: non-blocking IO, asynchronous server logic, Node.js, JavaScript, npm, target runtime, dependencies

3.1 The Node.js runtime

The section contrasts blocking file reads with Node.js’s single-threaded, non-blocking approach to handling requests.

Key terms: blocking IO, threads, overhead, single thread, callback, fs.readFile, non-blocking IO, Don’t block the thread!, promises, async/await

3.1.1 Running the Node.js REPL

This subsection shows how to explore Node.js in its REPL and why its global object looks different from a browser’s.

Key terms: node command, REPL, return value, console.log, global object, global variables, window, modules

3.1.2 Using Node.js APIs

This section uses a command-line calculator to introduce Node.js’s readline API and explains why the example file has an .mjs extension.

Key terms: readline API, callback-based, promise-based, REPL, .mjs, CommonJS, ECMAScript Modules (ESM), .js, require, import

3.2 Managing Node.js dependencies

Introduces how Node.js modules, node_modules, and npm made JavaScript code easier to load and share.

Key terms: modules, namespace, node_modules, npm, public registry, package manager

3.2.1 Installing dependencies with npm

The section creates a Node.js project, installs Express with npm, and runs a small server to use the new dependency.

Key terms: package.json, dependencies, npm registry, dependency tree, node_modules, package-lock.json, express, HTTP APIs, index.mjs, ESM module

3.2.2 Understanding node_modules

This section explains how Node.js resolves package imports through node_modules, how npm arranges dependencies there, and how local packages can be linked for import.

Key terms: core Node.js API, relative file path, bare specifier, node_modules, entry point, package.json, exports, main, indirect dependencies, nested node_modules

3.2.3 Syncing and updating dependencies

Explains how npm keeps dependency installations reproducible while allowing compatible updates, and how to update packages within or beyond their declared version ranges.

Key terms: package-lock.json, package.json, node_modules, lockfile, version ranges, semver, major, minor, patch, pre-release

3.2.4 Forking and patching dependencies

This section explains when to fork a dependency and how to preserve a smaller change with patch-package.

Key terms: fork, package.json, git link, npm install, node_modules, patch-package, .patch file, postinstall, --save-dev, devDependencies

3.3 Scripting with Node.js

This introduction explains why Node.js made JavaScript a practical language for scripting beyond the browser.

Key terms: Node.js, JavaScript, shell scripting

3.3.1 Defining scripts in package.json

This section shows how to define and run npm scripts in package.json, including arguments, special script names, package executables, and scripts that call other scripts.

Key terms: scripts, package.json, npm run, -- separator, npm <script>, test, start, restart, stop, prebuild

3.3.2 Writing executable scripts with Node.js

This section explains how to run Node.js files as executables, use dependencies and shell commands in scripts, and package a script for environments where dependencies cannot be installed.

Key terms: shebang, chmod +x, executable permissions, import statements, node_modules, module resolution, child_process, zx, $, bundler

3.3.3 Running package executables with npx

This section explains how npm packages expose command-line executables and how npx runs them locally or temporarily from the registry.

Key terms: bin, package.json, executable, npx, package executables, scripts entry, npx <command>, registry, node_modules, lockfile

3.4 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 2: Using modern JavaScript tools Chapter 4: Building web applications →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.