Node.js Basics for Beginners: Modules, File System, and Building Your First Server

A beginner-friendly guide to Node.js — covering modules, the file system API, HTTP servers, and npm — designed for developers ready to move JavaScript from the browser to the backend.

Endless Forge
Endless Forge
Apr 9, 20268 min read
Node.js Basics for Beginners: Modules, File System, and Building Your First Server
Image source: Node.js Basics for Beginners: Modules, File System, and Building Your First Server

Node.js Basics for Beginners: Modules, File System, and Building Your First Server

Node.js lets you run JavaScript outside of the browser — on your computer, on a server, or in the cloud. It’s what powers some of the busiest APIs on the internet, and it’s the runtime behind tools like npm, Next.js, and Vite that you’ve probably already used. If you know JavaScript and want to move into backend development, Node.js is one of the most natural and practical starting points.

This guide covers the core concepts you need to get productive quickly.


What is Node.js and how does it work?

Node.js is a JavaScript runtime built on Chrome’s V8 engine. It lets you execute JavaScript code server-side — reading files, starting HTTP servers, connecting to databases, and sending network requests.

Node.js is particularly well-suited for:

  • Building APIs and web servers
  • Running scripts and automation tasks
  • Handling real-time applications (chat, notifications, live updates)
  • Backend tooling and build processes

The key characteristic of Node.js is its non-blocking, event-driven architecture — it handles many operations concurrently without spawning a new thread for each one, which makes it efficient for I/O-heavy workloads.


Installing Node.js

Download the LTS (Long-Term Support) version from nodejs.org. Once installed, verify it works:

node --version
npm --version

You can run any JavaScript file directly:

node myfile.js

Modules: Organizing Your Code

Node.js uses a module system to organize code into separate files. There are two module formats: CommonJS (the original) and ES Modules (the modern standard).

CommonJS (older, still common):

// math.js
function add(a, b) {
  return a + b;
}
module.exports = { add };

// main.js
const { add } = require("./math");
console.log(add(2, 3)); // 5

ES Modules (modern, recommended for new projects):

// math.js
export function add(a, b) {
  return a + b;
}

// main.js
import { add } from "./math.js";
console.log(add(2, 3)); // 5

To use ES Modules, add "type": "module" to your package.json, or use the .mjs file extension.


npm: Managing Packages

npm (Node Package Manager) is included with Node.js and gives you access to millions of open-source packages. Every Node.js project starts with a package.json file:

npm init -y

Installing a package:

npm install express

This adds the package to node_modules and records it in package.json. To install a package only for development (like a testing framework):

npm install --save-dev nodemon

Run scripts defined in package.json:

{
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  }
}
npm run dev

The File System API: Reading and Writing Files

Node.js has a built-in fs module for working with the file system. The modern approach uses promises:

import { readFile, writeFile } from "fs/promises";

// Reading a file
const content = await readFile("./data.txt", "utf-8");
console.log(content);

// Writing a file
await writeFile("./output.txt", "Hello from Node.js!");
console.log("File written successfully");

Reading a JSON file (very common in Node.js projects):

import { readFile } from "fs/promises";

const raw = await readFile("./config.json", "utf-8");
const config = JSON.parse(raw);
console.log(config.appName);

Always use try/catch around file operations, since files can be missing or permissions may be denied:

try {
  const content = await readFile("./missing.txt", "utf-8");
} catch (error) {
  console.error("Could not read file:", error.message);
}

Building Your First HTTP Server

Node.js has a built-in http module. Here’s the simplest possible web server:

import { createServer } from "http";

const server = createServer((request, response) => {
  response.writeHead(200, { "Content-Type": "text/plain" });
  response.end("Hello, world!");
});

server.listen(3000, () => {
  console.log("Server running at http://localhost:3000");
});

Run it with node index.js and open your browser to http://localhost:3000. You have a running web server.

For real projects, you’ll use a framework like Express which adds routing, middleware support, and much cleaner syntax:

import express from "express";

const app = express();
app.use(express.json());

app.get("/", (req, res) => {
  res.send("Hello from Express!");
});

app.get("/users/:id", (req, res) => {
  const { id } = req.params;
  res.json({ userId: id, name: "Developer" });
});

app.listen(3000, () => {
  console.log("Express server running on port 3000");
});

Environment Variables: Configuration Done Right

Never hardcode sensitive values (API keys, database URLs, secrets) directly in your code. Use environment variables instead:

// .env file
PORT=3000
DATABASE_URL=mongodb://localhost/mydb
API_KEY=your-secret-key
// index.js — using dotenv to load .env
import "dotenv/config";

const port = process.env.PORT || 3000;
const apiKey = process.env.API_KEY;

console.log(`Running on port ${port}`);

Install dotenv: npm install dotenv. Add .env to your .gitignore so secrets never end up in version control.


Why Node.js Is Worth Learning

Node.js gives you a compelling combination of:

  • Unified language — JavaScript everywhere, frontend and backend
  • Massive ecosystem — npm has over 2 million packages
  • Performance for I/O tasks — event-driven architecture handles thousands of concurrent connections efficiently
  • Career value — Node.js skills transfer directly to Express, Next.js, Remix, Deno, and Bun
  • AI tooling — most AI SDKs (OpenAI, Anthropic, LangChain) have first-class Node.js support

Final Thoughts

The Node.js building blocks that will get you productive fastest:

  • Modules to organize and share code across files
  • npm to install and manage packages
  • File system API for reading, writing, and processing files
  • HTTP module and Express for building APIs and web servers
  • Environment variables for safe configuration management

Once you’re comfortable here, the path to databases (with Prisma or Drizzle), authentication, REST APIs, and full-stack frameworks like Next.js opens up naturally.


Upcoming articles will cover building a REST API with Express and a database, authentication patterns for Node.js, and deploying Node.js apps to the cloud.


Back to Blog