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





