JavaScript was born in the browser. Node.js takes the same language, removes the page, and gives it access to the file system, the network and the operating system. Every backend lesson in this track runs on it.
Learning objectives
- Explain what Node.js is and how it differs from browser JavaScript.
- Run a script with
node, and use the REPL for quick checks. - Choose between CommonJS and ES modules.
- Read command-line arguments and environment variables from
process.
1. What Node.js is
Node.js is a runtime built on Chrome's V8 engine. It executes JavaScript outside the browser and adds APIs the browser does not have:
| Browser | Node.js |
|---|---|
document, window |
fs, path, http |
localStorage |
files on disk, databases |
| DOM events | server requests, process signals |
It is single-threaded but non-blocking: slow work (disk, network) is handed to the system and the event loop continues with other callbacks.
2. Install and verify
node --version # v20.x or newer
npm --version
Start the REPL by typing node with no file. It evaluates one expression at a time — useful for checking a method without creating a file. Exit with .exit or Ctrl+C twice.
3. Running a file
// app.js
console.log("Hello from Node");
node app.js
Node does not have an HTML file to load, so the entry point is whatever file you pass.
4. CommonJS and ES modules
Node supports two module systems.
CommonJS — the default for .js files:
// math.js
function add(a, b) {
return a + b;
}
module.exports = { add };
// app.js
const { add } = require("./math");
console.log(add(2, 3));
ES modules — add "type": "module" to package.json, or use the .mjs extension:
// math.mjs
export function add(a, b) {
return a + b;
}
// app.mjs
import { add } from "./math.mjs";
Pick one system per project. Mixing require and import in the same file fails.
5. The process object
process describes the running program.
// node app.js hello world
console.log(process.argv);
// [ 'C:\\Program Files\\nodejs\\node.exe', 'D:\\app.js', 'hello', 'world' ]
const args = process.argv.slice(2); // ['hello', 'world']
console.log(process.env.NODE_ENV); // "development" if set
console.log(process.cwd()); // current working directory
process.exit(1); // stop with a failure code
Exit code 0 means success, anything else means failure. Scripts and CI pipelines depend on this.
6. Reading input
For interactive input, Node 18+ ships readline/promises:
import { createInterface } from "node:readline/promises";
const rl = createInterface({ input: process.stdin, output: process.stdout });
const name = await rl.question("Your name: ");
console.log(`Hello, ${name}`);
rl.close();
Common mistakes
- Running
nodeinside a folder with no entry file — pass the filename. - Using
windowordocumentin Node; they do not exist. - Forgetting that
process.argvincludes the Node binary and script path.
Practice
Build the command-line calculator in the assignment. It is the smallest possible program that uses arguments, validation, branching and exit codes — the skeleton of every CLI tool.