Node.js was created to solve a practical performance problem: building servers that avoid wasting CPU time while waiting on file or network operations. Instead of blocking on input/output, Node.js relies on a single-threaded model with callbacks, promises, and async/await to keep work moving and let other requests proceed. The chapter uses this idea to show how JavaScript, once seen mainly as a browser language, became a strong choice for server-side and system-level programming.
The chapter also introduces Node.js as a modular runtime with its own APIs, which are exposed through imports rather than as many browser-style globals. Through small examples, it shows how to use built-in modules such as readline and process to make a simple command-line calculator and how ESM module syntax works in practice. It also explains how Node.js resolves packages through node_modules, how package entry points are discovered, and how npm installs dependencies, records them in package.json and package-lock.json, and handles version ranges through semantic versioning.
Beyond installation, the chapter explores how developers manage and adapt dependencies in real projects. It covers updating packages, distinguishing runtime dependencies from dev dependencies, and modifying third-party code through forking or patching. Finally, it shows how Node.js supports scripting through package.json scripts, executable files with shebangs, and package binaries invoked with npm or npx, making JavaScript useful for everything from build automation to command-line tools and broader development workflows.
Summary
Node.js is a popular JavaScript runtime that provides APIs for the file system, networking, and other OS interactions.
JavaScript code always runs in a single thread. Callbacks and promises allow I/O operations to be performed asynchronously, keeping the JavaScript thread unblocked.
Node.js includes a package manager called npm, which installs packages into a node_modules directory containing a project’s entire dependency tree. It saves a list of top-level dependencies in package.json, and it also uses a file called package-lock.json to track the full contents of node_modules for version control.
When you import a module using a package name instead of a complete file path, Node.js attempts to find that package by recursing through the closest node_modules directory.
Node.js supports executable scripts with the shebang syntax. Packages can make executables available for consumption by declaring them in the bin field of package.json.
npm comes with a command called npx which can run executables from other packages. If the specified package isn’t installed, it will be downloaded from the npm registry and temporarily installed while the command runs.
FAQ
What problem was Node.js originally designed to solve?Node.js was created to improve server efficiency by using non-blocking I/O, so the CPU wouldn’t sit idle while waiting for disk or network operations to finish.Why does Node.js use callbacks or promises instead of blocking code for file and network work?Because JavaScript in Node.js runs on a single thread, callbacks and promises let long-running I/O operations finish asynchronously while the CPU continues handling other tasks.What is the REPL in Node.js?The REPL is the Read-Eval-Print Loop, an interactive environment you get by running node with no arguments, where you can type and execute JavaScript directly.Why does the Node.js global object look smaller than the browser’s window object?Node.js exposes many of its capabilities as modules instead of attaching lots of APIs to global variables, unlike browsers that attach many APIs to window.What is the purpose of the .mjs file extension?.mjs tells Node.js to treat the file as an ECMAScript Module (ESM) rather than CommonJS, which is Node.js’s default behavior for .js files.What does npm init -y do?It creates a basic package.json file with default values, saving you from answering interactive setup questions.What is the difference between dependencies and devDependencies in package.json?dependencies are needed when the app runs in production, while devDependencies are only needed during development, such as build or tooling packages.What is package-lock.json for?It records the exact dependency tree installed in node_modules, allowing npm to recreate the same versions consistently across machines and installs.What does the caret range ^5.1.0 mean in npm versioning?It means npm can install compatible newer versions with the same major version, such as 5.1.1 or 5.2.0, but not 6.0.0.What is npx used for?npx runs executables from installed packages, and it can also temporarily fetch and run a package from the npm registry even if it isn’t already installed locally.
pro $24.99 per month
access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!