Full-Stack Development · Tutorial 9 of 9
Node.js Basics: how the server side actually runs
By the FUTEAC Team · 17 November 2026 · 13 min read · Beginner
In short
Node.js runs JavaScript outside the browser, on your computer, with access to files, the network, and the command line. Run a file with node filename.js, read input from process.argv, work with files through fs, and answer web requests with the built-in http module. This tutorial runs each step, and shows the one module error every beginner hits.
- Node.js is JavaScript with a different job. The language is the same one you use in pages; the new part is access to files, networks, and processes.
- You run a file with
node filename.js. Node executes it top to bottom, prints what it prints, and exits. process.argvcarries command-line input. The first two entries are Node itself and your script; your arguments start at index 2.- The built-in
httpmodule can already serve a page. Express, in item 26, is a friendlier layer on top of it. - This tutorial feeds the Node & Express API Foundations course (/course/node-express-api-foundations) and the Node.js (/skill/node-js) skill.
Before you start
What is Node.js, really?
Node.js is JavaScript outside the browser. In a page, JavaScript can change the DOM and react to clicks, but it cannot read a file from your disk or listen for network requests. The browser does not allow it, for good reason: a random website should not be able to read your files. Node takes the same language, the same const, arrow functions, and array methods, and runs it on your computer without the browser's limits. Same language, new job.
Think of a chef who learned to cook at home and now runs a restaurant kitchen. The knife skills are the same. What changes is the equipment around them: a bigger stove, a walk-in fridge, orders coming in from the dining room. Browser JavaScript is the home kitchen, small and safe. Node is the restaurant kitchen: files, network sockets, and processes are the new equipment, and the language you already know is still doing the cooking.
Two parts make Node work. The engine is V8, the same JavaScript engine Chrome uses, so your code runs fast. Around it, Node adds a library of built-in modules for the things a server needs: fs for files, http for web requests, path for file paths, process for information about the running program. You will use three of those in this tutorial.
Check Node is installed before you start:
node --version
npm --versionOur run showed v24.22.1 and 10.9.4. Any recent version works for this tutorial. If the command is not found, install Node from nodejs.org, the LTS version, and run the commands again.
How do you run your first Node script?
Make a folder for the demo and a first file in it:
mkdir node-basics-demo
cd node-basics-demoCreate hello.js:
console.log("Hello from Node.js");
console.log("Node version:", process.version);Run it:
node hello.jsThe real output from our run:
Hello from Node.js
Node version: v24.22.1That is the whole pattern for the rest of this tutorial. Write a file, run it with node, read what it prints. Node runs the file top to bottom and exits when it finishes. That is different from a server, which stays running, and section 5 shows that difference. process is a built-in object that is always available, no import needed, and process.version is the version of the Node running your code.
How do you read input from the command line?
A script that only prints fixed text is a poster. A script that takes input is a tool. Node gives you the command-line arguments in process.argv, an array. The first entry is the path to the Node program itself, the second is the path to your script, and your arguments start at index 2.
Create greet.js:
const name = process.argv[2] || "stranger";
console.log(`Hello, ${name}!`);
console.log("Arguments received:", process.argv.slice(2));Run it with a name:
node greet.js AyeshaThe real output:
Hello, Ayesha!
Arguments received: [ 'Ayesha' ]Run it with no name and the fallback kicks in:
node greet.jsHello, stranger!
Arguments received: []Think of process.argv as a takeaway order slip. The first two lines are the kitchen's own notes, which program is running and which script it ran. Your order starts on line three. That is why the code reads index 2 and not index 0: a classic off-by-two mistake is reading process.argv[0] and greeting the path to Node instead of the person. The || "stranger" is a default for the case when no argument was given, so the script never prints Hello, undefined!.
How do you work with files?
Browser JavaScript cannot touch the file system. Node can, through the fs module. Create files.js:
import fs from "node:fs";
fs.writeFileSync("notes.txt", "Shopping list\n- bread\n- eggs\n");
const text = fs.readFileSync("notes.txt", "utf8");
console.log("File contents:");
console.log(text);
console.log("Done.");Run it:
node files.jsThe real output:
File contents:
Shopping list
- bread
- eggs
Done.And notes.txt now exists in the folder with exactly those three lines. Three details matter. First, import fs from "node:fs" uses the node: prefix, which marks a built-in module and keeps it distinct from any package you install later. Second, writeFileSync and readFileSync are the synchronous versions: they finish before the next line runs, which is the simplest shape for learning. The async versions exist and are what a real server uses, and they are a later step. Third, readFileSync needs "utf8" to return text; without it, it returns raw bytes, and console.log prints a buffer object instead of the words.
How do you run a server?
A server is a program that waits. It listens on a port, and every time a request arrives, it runs a function and sends a reply. Node's built-in http module does this with no packages to install. Create server.js:
import http from "node:http";
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello from my Node server!\n");
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});Start it:
node server.jsThe terminal prints Server running at http://localhost:3000 and then waits: the prompt does not come back, because the server is listening. That waiting is the difference from the earlier scripts. Open a second terminal and ask it for a page:
curl http://localhost:3000The real output:
Hello from my Node server!Or open http://localhost:3000 in a browser and the same line shows on the page. Stop the server with Ctrl+C in the first terminal.
Three details matter. The port, 3000, is the door the server listens on, and the URL has to name it. res.writeHead(200, ...) sends the status code and headers before the body: 200 means success, and text/plain tells the browser the reply is plain text. And res.end() is what actually sends the reply. Forget it and the browser waits forever, which is one of the common mistakes below.
This is the honest minimum of a web server, and it is worth seeing before Express. Item 26 built a five-endpoint tasks API with Express, and it said Express is routing plus conveniences on top of this exact module. Now you have seen the layer underneath: createServer, a function per request, and a reply built by hand.
The wrong turn: require is not defined
Here is the error every beginner hits, and we reproduced it while testing this tutorial. Node has two module systems: the older CommonJS, with require, and the newer ES modules, with import. The files above use import, so they are ES modules. Now write a file that mixes the two:
const fs = require("node:fs");
console.log("This never prints.");Save it as wrong.js in a folder whose package.json contains "type": "module", and run it:
node wrong.jsThe real error:
ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '.../node-basics-demo/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.Node even tells you the fix, which is rare for an error message and worth reading properly. There are two ways out. The one this tutorial uses: write import fs from "node:fs" instead of require, as files.js already does. The other: rename the file to .cjs, which marks it as CommonJS. We checked that too: the same code saved as wrong.cjs runs and prints its line with exit code 0.
The rule is simple. In a project with "type": "module", use import. In a project without it, use require. Mixing them is the mistake, and the error message names the cause, so read it instead of guessing. Most tutorials and Stack Overflow answers you will meet use one system or the other without saying which, so when a snippet fails, check which module system the snippet assumes before anything else.
How do modules and npm fit in?
A module is a file that exports something for other files to use. You saw the built-in side of that: import fs from "node:fs". Your own files work the same way. Create greeting.js:
export function greet(name) {
return `Hello, ${name}!`;
}And use it from a second file, main.js:
import { greet } from "./greeting.js";
console.log(greet("Ayesha"));Run node main.js and it prints Hello, Ayesha!. Note the ./ and the .js extension in the import: Node needs the full relative path, including the extension, for your own files. Leaving off .js is a common error, and the message will say it cannot find the module.
package.json is the file that describes your project: its name, its version, and whether it uses ES modules or CommonJS. Create one with:
npm init -yIt writes a package.json with defaults, including "version": "1.0.0". Add "type": "module" to it, and every .js file in the folder becomes an ES module. That one line marks every file as a module explicitly, so import works the same way in every file in this tutorial, on any Node version. npm itself is the package installer that comes with Node: npm install express downloads Express into a node_modules folder and records it in package.json. You do not need it yet. Item 26 is where Express arrives, and it installs exactly one package for the whole API.
Try it yourself
1. Change greet.js to greet two names: read process.argv[2] and process.argv[3], and print a line for each. Run it with two names and check both lines print. 2. Change server.js to answer differently per path: if req.url is /about, reply "This is the about page."; otherwise reply the hello line. Test both paths with curl. 3. Write a script that reads notes.txt and prints how many non-empty lines it has. Check the number against the file: three lines of text. 4. Move the greeting function into greeting.js as above, import it in main.js, and run it. Then remove the .js from the import and read the error Node prints.
Continue in the free course: Node & Express API Foundations →
Common mistakes
- Using
requirein an ES module project. You getReferenceError: require is not defined in ES module scope. Useimport, or rename the file to.cjs. - Reading
process.argv[0]or[1]as your input. Those are Node and your script. Your arguments start at index 2. - Forgetting
res.end()in a server. The browser waits forever with no error. Every request handler must end the response. - Reading a file without
"utf8"and wondering why the output looks wrong. Without an encoding,readFileSyncreturns a buffer of raw bytes, not text. - Starting the server twice and hitting a port clash. The second one fails with
EADDRINUSE: address already in use. Stop the first server withCtrl+Cbefore starting another on the same port.
Frequently asked questions
Is Node.js a language?
No. It is a runtime: a program that runs JavaScript outside the browser. The language is JavaScript, and everything you know from the frontend still applies. What is new is the environment: files, network, and processes instead of the DOM.
Do I need to learn Node before Express?
Yes, in this order. Express is a library that runs on Node, and its errors are often Node errors in disguise. After this tutorial you can read the server code in item 26 and see what is Express and what is plain Node underneath it.
Can Node serve a real website?
Yes, and this tutorial's server is the smallest version of one. Real apps add Express for routing, a database for storage, and a hosting service to keep the server running. The FUTEAC platform itself runs its API on Node, which is why the full-stack roadmap teaches it.
Why are there two module systems?
require came first, and import arrived with ES6. The ecosystem is moving to import, and new projects should use it, but older code and many packages still use require. You will meet both. Knowing which one your project uses is the difference between code that runs and the error in section 6.
What is the difference between Node and npm?
Node runs JavaScript. npm installs packages and manages your project. They are installed together, and npm --version checks the package side the way node --version checks the runtime side.
Follow the Full-Stack Developer Roadmap
Build complete web applications, frontend, backend APIs, databases and deployment. Free, step by step, at your pace.
View the Roadmap