3.1 — Quick Revision: Starting with Node.js
A compact cheat sheet covering Node.js fundamentals, setup, globals, npm, and package.json.
< Interview Questions | README
How to use this material
- Read through once after completing all subtopics (3.1.a through 3.1.e).
- Cover the right column of each table and try to recall the answer from the left column.
- Print this page for quick reference before interviews or exams.
- Flag anything you cannot recall and revisit that specific lesson file.
Core Vocabulary
| Term | Definition |
|---|
| Node.js | JavaScript runtime built on V8 + libuv + Node APIs — runs JS outside the browser |
| V8 | Google's C++ JavaScript engine — compiles JS to machine code via JIT |
| libuv | C library providing the event loop, thread pool (4 default), and async I/O |
| Event loop | Single-threaded loop that delegates I/O and processes callbacks from queues |
| Non-blocking I/O | I/O operations are delegated and do not block code execution |
| npm | Node Package Manager — registry (2M+ packages) + CLI tool + website |
| npx | Run packages without global installation — always uses latest version |
| SemVer | MAJOR.MINOR.PATCH versioning — ^ allows minor+patch, ~ allows patch only |
| LTS | Long-Term Support — even-numbered Node.js versions (18, 20, 22) |
| REPL | Read-Eval-Print-Loop — interactive Node.js shell (node with no args) |
| CommonJS | require() / module.exports — Node.js original module system |
| ES Modules | import / export — the modern JavaScript standard |
Node.js vs Browser JavaScript
| Feature | Browser | Node.js |
|---|
| Global object | window | global |
| Universal global | globalThis | globalThis |
| DOM | document, window.document | Not available |
| File system | Not available | fs module |
| HTTP server | Not available | http module |
| Module system | ES Modules | CommonJS + ES Modules |
| Process control | Not available | process object |
__dirname | Not available | Available (CJS only) |
npm Commands Cheat Sheet
| Command | What It Does |
|---|
npm init -y | Create package.json with defaults |
npm install / npm i | Install all dependencies from package.json |
npm i express | Install package (adds to dependencies) |
npm i nodemon -D | Install as devDependency |
npm i -g typescript | Install globally |
npm i express -E | Install exact version (no ^ or ~) |
npm uninstall express | Remove a package |
npm update | Update packages within SemVer range |
npm outdated | Show which packages have newer versions |
npm ls --depth=0 | List top-level installed packages |
npm audit | Check for security vulnerabilities |
npm audit fix | Auto-fix vulnerabilities |
npm ci | Clean install from lock file (for CI/CD) |
npm run <script> | Run a custom script from package.json |
npm start | Run the "start" script (no "run" needed) |
npm test / npm t | Run the "test" script (no "run" needed) |
npx <package> | Run a package without installing globally |
nvm Commands Cheat Sheet
| Command | What It Does |
|---|
nvm install --lts | Install latest LTS version |
nvm install 20 | Install latest Node.js 20.x |
nvm use 20 | Switch to Node.js 20.x |
nvm ls | List installed versions |
nvm alias default 20 | Set default for new terminals |
nvm current | Show active version |
nvm uninstall 16 | Remove a version |
nvm use | Use version from .nvmrc |
node Command Cheat Sheet
| Command | What It Does |
|---|
node app.js | Run a JavaScript file |
node | Start the REPL |
node -v | Print Node.js version |
node -e "code" | Execute inline code |
node -p "expr" | Evaluate and print result |
node --check app.js | Syntax check without running |
node --watch app.js | Auto-restart on file changes (Node 18+) |
node --env-file=.env app.js | Load .env file (Node 20.6+) |
package.json Key Fields
| Field | Purpose | Example |
|---|
name | Package name (lowercase, no spaces) | "my-api" |
version | SemVer version | "1.0.0" |
description | One-line summary | "REST API for my app" |
main | Entry point for require() | "src/index.js" |
type | Module system | "module" or "commonjs" |
scripts | Custom npm commands | { "start": "node server.js" } |
dependencies | Runtime packages | { "express": "^4.18.2" } |
devDependencies | Development-only packages | { "jest": "^29.7.0" } |
peerDependencies | Consumer must provide | { "express": "^4.0.0" } |
engines | Required Node/npm version | { "node": ">=20" } |
private | Prevent accidental publishing | true |
exports | Modern entry point map | { ".": { "import": "..." } } |
Node.js Global Objects and Variables
| Global | What It Is |
|---|
process | Current process info — argv, env, cwd(), exit(), pid |
console | Output methods — log, error, warn, table, time |
__dirname | Absolute path to the directory of the current file (CJS only) |
__filename | Absolute path to the current file (CJS only) |
global | The global namespace object |
globalThis | Universal global (works in browser and Node.js) |
Buffer | Handle binary data |
setTimeout | Schedule a callback after a delay |
setInterval | Schedule a recurring callback |
setImmediate | Schedule a callback in the check phase |
require() | Import a module (CJS only) |
module | Reference to the current module (CJS only) |
Dependency Types Quick Reference
| Type | In production? | Install command | Example packages |
|---|
| dependencies | Yes | npm i express | express, mongoose, bcrypt, cors |
| devDependencies | No | npm i jest -D | jest, eslint, nodemon, prettier |
| peerDependencies | Must exist | Consumer installs | express (for plugins) |
SemVer Range Symbols
| Symbol | Meaning | ^4.18.2 allows |
|---|
^ (caret) | Minor + patch updates | >=4.18.2 <5.0.0 |
~ (tilde) | Patch updates only | >=4.18.2 <4.19.0 |
| (none) | Exact version | Only 4.18.2 |
* | Any version | Latest |
CommonJS vs ES Modules
| Feature | CommonJS | ES Modules |
|---|
| Import | require('pkg') | import pkg from 'pkg' |
| Export | module.exports = ... | export default ... |
| Loading | Synchronous | Asynchronous |
| Top-level await | No | Yes |
| Tree-shaking | No | Yes |
__dirname | Available | Use import.meta.url |
| File extension | .js or .cjs | .js (with "type":"module") or .mjs |
| Default in Node.js | Yes | Opt-in via "type":"module" |
Event Loop Phases (Simplified)
┌─► timers ← setTimeout, setInterval callbacks
│ pending ← deferred I/O callbacks
│ poll ← retrieve + execute I/O callbacks
│ check ← setImmediate callbacks
│ close ← socket.on('close') callbacks
└───────────────── (repeat)
Microtasks (process.nextTick, Promises) run BETWEEN every phase.
Execution order:
- Synchronous code
process.nextTick callbacks
- Resolved Promise callbacks (
.then)
setTimeout / setImmediate callbacks
Master Workflow: New Project Setup
mkdir my-project && cd my-project
echo "20" > .nvmrc && nvm use
npm init -y
echo "node_modules/\n.env\n.DS_Store\ndist/\ncoverage/" > .gitignore
touch index.js
npm i express dotenv cors
npm i -D nodemon jest eslint
git init && git add -A && git commit -m "Initial commit"
npm start
npm run dev
Files to Commit vs Ignore
| Commit | Ignore (.gitignore) |
|---|
package.json | node_modules/ |
package-lock.json | .env |
.nvmrc | .DS_Store |
.gitignore | dist/ |
Source code (src/) | coverage/ |
<- Back to 3.1 — Starting with Node.js (README)