Built-In Modules: fs, path, http
Built-In Modules: fs, path, http
Node ships with a standard library covering files, paths, and networking. Three modules appear in almost every project: fs, path, and http.
fs for the filesystem
fs reads and writes files. The modern promise API avoids callback nesting:
import { promises as fs } from 'fs';
const text = await fs.readFile('notes.txt', 'utf8');
await fs.writeFile('copy.txt', text);
There are also sync flavors for scripts where blocking does not matter, like reading config at startup.
path for safe paths
path joins parts together with the correct separator for the operating system:
import path from 'path';
const dir = path.join(__dirname, 'uploads');
console.log(path.extname('photo.jpg')); // .jpg
Using path.join instead of string concatenation avoids broken paths on Windows versus Linux.
http for servers
http creates a minimal server without frameworks:
import http from 'http';
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Node');
});
server.listen(3000);
The callback receives the request and response objects. Every incoming request calls it, which is all a web server fundamentally needs.
Console, process, and beyond
console prints output, and process exposes environment, arguments, and exit codes. Together the built-ins cover most low-level needs, and frameworks like Express build on top of them.
Key Points
- fs handles reading and writing files.
- path builds cross-platform paths.
- http creates raw servers without frameworks.
- Promise APIs keep async code readable.
- process exposes runtime and environment data.