Node.js Basics for Beginners

If you already know a little JavaScript, Node.js is a natural next step. It lets you run JavaScript outside a web browser, usually on a server or on your own computer. That means you can build command-line tools, small websites, application programming interfaces (APIs), and automation scripts using the same language you use for web pages.
The basics are simple: you write JavaScript in a file, run it with Node.js, and see the result. Once you understand that cycle, everything else becomes easier.
This guide explains what Node.js is, why developers use it, how to check that it is installed, and how to write your first few programs.
What Is Node.js?
Node.js is a runtime environment for JavaScript. A runtime is simply the software that executes your code. Browsers have their own JavaScript runtimes. Node.js gives JavaScript a runtime for computers and servers, without needing a browser window.
Node.js is built on the V8 engine, the same engine that powers JavaScript in Chrome. It adds useful features for working with files, networks, and operating systems. For example, in a browser, JavaScript cannot freely read files from your computer for security reasons. In Node.js, reading and writing files is a normal part of many programs.
It is important to understand that Node.js is not a programming language and it is not a framework. It is a tool that runs JavaScript. Frameworks such as Express are built on top of Node.js, but you do not need a framework to start learning.
Why Do Developers Use Node.js?
One major reason is consistency. If you learn JavaScript for the front end, you can reuse much of that knowledge on the back end. You do not need to learn a completely different language just to build a server.
Node.js is also well suited to tasks that involve many small operations happening at the same time, such as handling web requests, reading data, or chatting with a database. Its non-blocking design means it can start one task, move on to another while waiting, and return to the first task when it is ready. This helps programs stay responsive.
Another advantage is the large ecosystem around Node.js, including thousands of reusable packages and countless tutorials. That does not mean it is right for every project, but for learning back-end development it is an accessible starting point.
Checking Your Installation
Before writing code, make sure Node.js is installed. Open a terminal and run:
node --version
npm --version
If both commands return version numbers, you are ready to go. The first command checks Node.js itself. The second checks npm, the package manager that comes with Node.js. If either command fails, install the current long-term support (LTS) version of Node.js from the official website and try again.
Any basic code editor will do for your first programs.
Your First Node.js Program
Create a file called hello.js and add this code:
console.log("Hello from Node.js!");
Run it from the terminal in the same folder:
node hello.js
You should see the message printed in the terminal. That is the basic workflow: write code, save the file, and run it with the node command.
Try a slightly more interesting example:
const name = "Alex";
const year = 2026;
console.log(`Hello, ${name}!`);
console.log(`The year is ${year}.`);
Here, const creates a variable that will not be reassigned, and template literals (backticks) let you insert values into a string with ${}.
Working with Modules
As programs grow, it helps to split code into separate files. Node.js uses modules for this. A module is simply a file that exports some code so another file can use it.
For example, create a file called greet.js:
function greet(name) {
return `Hello, ${name}!`;
}
module.exports = greet;
Then use it in another file called app.js:
const greet = require("./greet");
console.log(greet("Sam"));
Run node app.js and you should see the greeting. The require function loads the module, and module.exports decides what the module shares. This pattern keeps code organised and easier to test.
Node.js also includes built-in modules. For example, the fs module works with files, and the path module helps build file paths safely across operating systems:
const path = require("path");
console.log(path.join("folder", "file.txt"));
A Tiny Web Server
One of the most exciting first projects is a small web server. Node.js includes a built-in http module, so you do not need to install anything extra:
const http = require("http");
const server = http.createServer((req, res) => {
res.writeHead(200, { "Content-Type": "text/plain" });
res.end("Hello from my Node.js server!");
});
server.listen(3000, () => {
console.log("Server running at http://localhost:3000");
});
Save this as server.js, run it with node server.js, and open http://localhost:3000 in your browser. You should see the message. Press Ctrl + C in the terminal to stop the server.
Real applications usually use a framework and a database, but this tiny server shows the core idea: Node.js receives a request, runs your code, and sends back a response.
Common Beginner Mistakes
Do not try to learn everything at once. Focus on variables, functions, objects, arrays, and modules first; frameworks and databases are easier after that. Also, do not copy code without understanding it — change a value, remove a line, and see what happens. Finally, read error messages carefully: they often point to the exact file and line where something went wrong.
What to Learn Next
Once you are comfortable with scripts and a tiny server, learn npm, then Express for APIs and async/await for asynchronous code. Small projects — a to-do list, a simple API, or a file-organising script — teach more than tutorials alone.
Conclusion
Node.js lets you run JavaScript beyond the browser, opening the door to servers, tools, and automation. Start with the basics: check your installation, run small scripts, use modules, and build a tiny server. Write a little code, run it, and build your understanding one program at a time.