Take JavaScript outside the browser — build servers, read files, and create command-line tools with Node.js.
What is Node.js?
Modules
File System
HTTP Server
NPM
Quiz
What is Node.js?
Imagine JavaScript is an employee who only works in one office (the browser). Node.js gives that employee the ability to work anywhere — servers, command line, file systems, databases. Node.js is not a new language; it's the same JavaScript you already know, but running outside the browser.
Key facts about Node.js:
Same language — It's JavaScript! Variables, functions, arrays, objects — all the same
Built on Chrome's V8 engine — The same engine that runs JS in Chrome
Non-blocking I/O — Can handle thousands of requests without waiting (async by nature)
Huge ecosystem — Over 2 million packages on NPM
Try It! Basic JavaScript in Node
These playgrounds run JavaScript just like Node.js would. Try them out:
JavaScript — Node.js Basics
What Node CAN'T do: Node.js does not have document, window, or alert() — those are browser-only. Instead, Node has access to the file system, network, and operating system.
Modules
Modules are like LEGO bricks. Each brick does one thing (a wheel, a window, a door), and you snap them together to build something complex. In Node.js, each file is a module — it exports what it wants to share, and other files import what they need.
CommonJS (require/module.exports)
This is the traditional Node.js way of importing and exporting:
Node.js — CommonJS Modules
ES Modules (import/export)
The modern way — same syntax you use in React:
Node.js — ES Modules
Practice: Working with Functions and Objects
JavaScript — Functions & Objects Practice
File System
The File System module (fs) gives Node.js the power to read, write, and manage files on your computer — like a digital filing cabinet. The browser can't do this (for security reasons), but Node can because it runs on the server.
Node.js — File System (fs) Module
Practice: Working with JSON Data
JavaScript — JSON Parse & Stringify
HTTP Server
An HTTP server is like a receptionist at an office. People (browsers) walk in and ask for things ("I want the homepage," "I want user data"). The receptionist listens, figures out what they want, and hands them the right response. Node.js can be that receptionist!
Node.js — Creating an HTTP Server
This is how every website works! When you visit google.com, your browser sends a GET request to Google's server. The server processes it and sends back HTML. Node.js lets YOU build that server.
Practice: Request and Response Concepts
JavaScript — Simulating Request/Response
NPM (Node Package Manager)
NPM is like an app store for code. Need to send emails? There's a package for that. Need to hash passwords? There's a package. Need to connect to a database? Package. Instead of building everything from scratch, you install packages that other developers have already built and tested.
Essential NPM Commands
Terminal — NPM Commands
package.json — Your Project's ID Card
package.json — Example
Never commit node_modules! This folder can contain thousands of files. Always add node_modules/ to your .gitignore. Anyone who clones your project can run npm install to get the packages.
Practice: Array Methods (Used Everywhere in Node)
JavaScript — Array Methods for Backend Dev
📝 Chapter 11 Quiz
1. What is Node.js?
A new programming language
A runtime that lets JavaScript run outside the browser
A CSS framework
A database system
2. Which function imports a module in CommonJS (traditional Node.js)?
import()
include()
require()
load()
3. Which Node.js module is used to read and write files?
fs
file
io
disk
4. What does NPM stand for?
New Programming Method
Node Program Manager
Network Package Module
Node Package Manager
5. Why should you never commit node_modules/ to Git?