When you install Node.js, you don’t just get the ability to run JavaScript outside the browser. You also get a set of core modules — built-in tools that let you interact with your computer and the internet. Unlike external packages you install with npm, these modules come ready to use.
💡 Before diving in: if you don’t already have Node.js installed, you can start with the simple setup in Getting Started with Node.js: Setup and Hello World. For developers who want the professional way to manage Node.js versions, check out my guide: Node Version Manager (nvm): A Developer’s Guide to Installing and Managing Node.js Versions.
👉 Tip: For each example below, make sure any mentioned files or folders (like example.txt or files/data.json) exist in your project before running the code.
We’ll look at three of the most useful core modules: fs, path, and http.
📂 The fs Module (File System)
The fs module lets you work with files — reading, writing, and updating them.
Example: Reading a File
const fs = require('fs');
// Make sure example.txt exists in the same folder
fs.readFile('example.txt', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
console.log(data);
});
This reads the contents of example.txt and prints it to the console.
🛤️ The path Module
The path module helps you work with file and folder paths in a way that works across operating systems.
Example: Joining Paths
const path = require('path');
// Assumes there's a folder called "files" with a file named data.json
const fullPath = path.join(__dirname, 'files', 'data.json');
console.log(fullPath);
This prints the full path to data.json, with the correct separators for your system.
🌐 The http Module
The http module lets you create web servers. With just a few lines of code, you can respond to requests in a browser.
Example: A Simple Web Server
const http = require('http');
// No extra files needed — just run this script
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Node.js!');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000');
});
Open your browser at http://localhost:3000 to see the response.
✅ Key Takeaway
- fs: Work with files (read/write).
- path: Safely handle file paths.
- http: Create web servers.
These modules are the foundation of many Node.js projects. They let JavaScript interact with your computer and the web without needing any extra tools.