← Full Stack (JavaScript)
Day 1 · Node.js and Express: Modules, npm, the Event Loop, fs/path and Middleware
Full Stack (JavaScript) · Crash Course
Day 1

Node.js and Express: Modules, npm, the Event Loop, fs/path and Middleware

⏱ About 90 minutes · 26 pages
By the end of today you will be able to
  • ✔ Explain what Node.js is and why it handles many users with one thread
  • ✔ Use CommonJS and ES modules, npm, package.json and semantic versions correctly
  • ✔ Predict the output order of timers, promises, nextTick and setImmediate
  • ✔ Build an Express server with routes, middleware and error handling
  1. What Node.js really is
  2. Running Node.js and its global objects
  3. CommonJS modules: require and module.exports

What Node.js really is

Nearly every full-stack round starts with "What is Node.js?", and a clear two-line answer sets the tone for the whole interview.

Node.js is a JavaScript runtime: it runs JavaScript outside the browser, on a server or your laptop. It is not a language and not a framework; it is an engine plus a toolbox for reading files, opening network ports and talking to databases.

Node.js is built from two main parts:

  • V8, the JavaScript engine from Google Chrome. V8 compiles your JavaScript into fast machine code.
  • libuv, a C library that gives Node.js its event loop and a small pool of background threads for slow work such as file reads, DNS lookups and compression.

On top sit the core modules and your own code.

Your app code (routes, logic) Core modules: fs, path, http, events, os V8 engine (runs JS) libuv (event loop, threads) Operating system: files, network, timers

One thread, many users

Your JavaScript runs on one main thread. Most web work is waiting: for the database, a file or another API. Node.js hands the slow job to the operating system or libuv, moves on to the next request, and runs your callback when the result is back. This is non-blocking, event-driven I/O.

Think of one waiter at a busy Chennai restaurant. He passes table 1's order to the kitchen and moves to table 2 instead of standing at the stove. When the bell rings, he delivers the dish.

Node.js suits APIs, real-time apps (chat, live scores) and build tools. It struggles with heavy CPU work on the main thread.

✍️ Example 1 — Why a CPU loop hurts everyone
  1. One request runs a loop that adds numbers for 3 seconds on the main thread.

  2. While that loop runs, the event loop cannot pick up any other callback.

  3. So every other user waits those 3 seconds too, even for a tiny request.

  4. Answer: CPU-heavy work blocks all users; move it to a worker thread or a separate service.

Full Stack (JavaScript) · Day 12 / 5

Running Node.js and its global objects

Knowing how to run a script and read process is the first practical skill an interviewer checks when you share your screen.

After installing Node.js you get two commands: node and npm. Check them with node -v and npm -v.

Run code in the REPL (type node), from a file (node app.js) or inline (node -e "...").

Globals you get without requiring anything

Global What it gives you
console log, error, table, time/timeEnd
process argv, env, exit(), cwd(), pid, nextTick()
__dirname, __filename Folder and full path of the current file (CommonJS only)
setTimeout, setInterval, setImmediate Timers
Buffer Raw binary data

There is no window and no document in Node.js, because there is no browser page. The global object is called global, and the standard name that works everywhere is globalThis.

Reading command-line arguments

process.argv is an array. Index 0 is the path to the node program, index 1 is the path to your script, and your own arguments start at index 2.

const argv = ["/usr/bin/node", "/home/arjun/greet.js", "Arjun", "Pune"];
const [, , name, city] = argv;
console.log(`Hello ${name} from ${city}`);
console.log(argv.slice(2).length);
Hello Arjun from Pune
2

In a real run, node greet.js Arjun Pune fills process.argv the same way.

✍️ Example 2 — Reading an environment variable with a default
  1. You run PORT=4000 node server.js.

  2. Inside the code, process.env.PORT is the string "4000", not a number.

  3. Write const port = Number(process.env.PORT) || 3000;

  4. Answer: port is 4000; if PORT were missing, it would fall back to 3000.

Full Stack (JavaScript) · Day 13 / 5

CommonJS modules: require and module.exports

Most Node.js codebases and interview snippets still use require, so you must know exactly what it returns.

A module is just a file. Each file has its own private scope, so a variable you declare in cart.js does not leak into app.js. To share something, you export it; to use it, you require it.

Exporting and importing

// gst.js
const RATE = 0.18;
function withGst(amount) {
  return amount + amount * RATE;
}
module.exports = { withGst, RATE };
// app.js
const { withGst } = require("./gst");
console.log(withGst(1000)); // 1180

require returns the other file's module.exports. ./ means a local file; without it, Node.js looks for a core module or a package in node_modules.

exports versus module.exports

exports is a shortcut pointing to the same object as module.exports. Adding properties works; reassigning exports breaks the link.

const module1 = { exports: {} };
let exports1 = module1.exports;
exports1.a = 1;
exports1 = { b: 2 };
console.log(module1.exports);
{ a: 1 }

This copies what Node.js does internally: require would return { a: 1 }.

Modules are cached

The first require("./db") runs the file; later calls return the same object without re-running it, so one DB connection is shared app-wide.

✍️ Example 4 — Counting how often a module runs
  1. counter.js contains console.log("loaded"); module.exports = { n: 0 };

  2. app.js calls const a = require("./counter"); const b = require("./counter"); a.n++;

  3. "loaded" prints once, because the second call hits the cache.

  4. a and b are the same object, so b.n is also 1.

  5. Answer: "loaded" prints once and b.n is 1.

Full Stack (JavaScript) · Day 14 / 5
Free preview ends here

Keep going with the full course

Day 1 has 26 pages. You have read the first 3.

See what's inside →