liveBook · Manning

Node.js

As explained in Secrets of the JavaScript Ninja, Third Edition

A standalone JavaScript runtime built around the V8 engine and designed for server-side and general-purpose applications.

What happens when a server reads a file while other requests are waiting? Node.js lets JavaScript handle work outside the browser, including servers, command-line programs, and development tools. It is a runtime built on the V8 engine; its APIs connect JavaScript to the file system, network, processes, and other operating-system facilities. section 1.1.1 section 3.0

A file read need not leave the JavaScript thread waiting. A request handler can start the read and respond when its callback runs, leaving the thread available for other work meanwhile. Promises and async/await express the same non-blocking I/O with more linear-looking code. That appearance can be misleading: only one part of the JavaScript program runs at a time, so a long computation can still block other callbacks. section 3.1

A command-line calculator

An index.mjs program can import node:readline/promises and node:process, ask for an expression, and print its result. Run it with node index.mjs. The .mjs extension marks it as an ECMAScript Module; Node.js otherwise treats .js files as CommonJS by default for backward compatibility. The sample calculator uses eval, so its input can execute arbitrary JavaScript—not just arithmetic. section 3.1.2

Listing 3.1 A calculator program in Node.js
import * as readlinePromises from "node:readline/promises";  #A
import { stdin, stdout } from "node:process";  #B
 
const rl = readlinePromises.createInterface({
  input: stdin,
  output: stdout,
});
 
while (true) {
  const input = await rl.question('Enter expression or ".exit":\n> ');  #C
  if (input === ".exit") {
    rl.close();
    break;
  } else {
    console.log(eval(input));  #D
  }
}

The complete calculator shows promise-based input, built-in module imports, and a loop that exits when the user enters .exit. section 3.1.2

Beyond running a program, Node.js supports a practical project workflow:

  • Use npm to install dependencies. Packages live under node_modules; package.json records top-level dependencies, while package-lock.json tracks the installed dependency tree. section 3.2 section 3.4
  • Write system scripts in JavaScript. On Mac/Linux, a shebang and executable permission let a script run directly; npx can run executables supplied by npm packages. section 3.3.2 section 3.4

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Explains non-blocking file reads through callbacks and async/await, then exercises APIs and packages; defers detailed promises and workers.
Examples
Uses a file-serving request handler, a Readline calculator, and shebang-based executable scripts.
What it assumes
Assumes Node.js is installed and node, npm, and npx are available; introduces promise-based APIs before treating promises in detail.
What you can do after
Apply it via Node.js to run command-line programs, manage packages with npm, and write executable scripts.
How it connects
Connects V8 and host APIs with asynchronous I/O, ECMAScript Modules, CommonJS, npm, and system scripting.

Also explained in

Compare how 6 books explain Node.js →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.