Voici la traduction de l'article du russe vers le français, en respectant toutes les règles spécifiées :
What is it and why? (a real-life analogy)
Imagine you're learning to cook. First you learn to chop vegetables (variables), then fry eggs (conditions), then cook soup (loops). You know many individual recipes. But real mastery comes when you can prepare a full three-course meal, where everything is cooked simultaneously, doesn't burn, and is served hot.
The final practical project is exactly that "full-course meal" in programming. It's a program that combines everything you've learned: variables, functions, arrays, objects, file handling, error handling. It solves a real problem, not just demonstrates how some single command works.
"A project is not an exam. It's your portfolio. Something you can show to friends and say: 'Look, I made this!'"
Why is this needed? Firstly, to consolidate knowledge. When you write code for a project, you're not just repeating — you're thinking about how to connect the pieces. Secondly, to understand how real programs work. In real life, nobody writes "a problem about cats" — they write applications, websites, bots. Thirdly, it's your first step into a portfolio. Even a simple project, done neatly, shows an employer: "This person knows how to see things through to the end."
In this article, we'll break down how to design and write a final project in JavaScript using Node.js. We won't be learning new commands — we'll learn how to assemble them into a working system.
Theory: how it works from the inside
Any project, even the most complex one, consists of three levels:
- Input data — what the program works with (files, key presses, internet requests).
- Processing logic — how the program transforms this data into a result (calculations, search, sorting).
- Output data — what the program gives to the user (saved file, console response, graph).
In Node.js (this is JavaScript that runs not in a browser, but on a computer) we have superpowers: we can read and write files, listen to ports, run other programs. But the main thing is we have a modular system. It's like a Lego set: each brick (module) does its own thing, and we assemble them into a common figure.
Inside a project, the code is usually divided into files:
index.jsormain.js— the entry point where everything starts.utils.js— helper functions (e.g., date formatting).data/— folder with data files (if any).package.json— file with the project description (like a passport).
When you run a project with the command node index.js, the following happens:
- Node.js reads the
index.jsfile. - It sees
require()commands — it connects other files and libraries. - It executes the code from top to bottom.
- If the program should run for a long time (e.g., a server), it doesn't terminate — it waits for events.
The most important thing in a project is architecture. This isn't a scary word. It's simply the answer to the question: "How is everything arranged?" Good architecture is when you can easily add a new feature without breaking an old one. Bad architecture is when everything is in one file and you're afraid to change anything.
"Good code is code that is easy to read. Great code is code that is easy to change."
Syntax in JavaScript (Node.js): all forms and variants
In a project, we don't use some special syntax, but a combination of already familiar things. Let's recall the main tools we'll need.
1. Modules (connecting files)
In Node.js, there are two ways to connect modules: the old one (CommonJS) and the new one (ES Modules). We'll use the old one — it's simpler and works everywhere.
// Connecting the built-in module for working with the file system
const fs = require('fs');
// Connecting our own file (e.g., helpers.js)
const helpers = require('./helpers.js');
// Connecting a library installed via npm (e.g., chalk for colors)
const chalk = require('chalk');
2. Reading and writing files
This is the foundation of any project that stores data.
// Reading a file (synchronous — the program waits until the file is read)
const data = fs.readFileSync('data.txt', 'utf8');
// Writing to a file (creates the file if it doesn't exist, or overwrites it)
fs.writeFileSync('output.txt', 'Hello, world!');
// Appending to the end of a file (doesn't overwrite, but adds to it)
fs.appendFileSync('log.txt', 'New entry\n');
3. Working with JSON
JSON is a data format that both humans and computers understand. In projects, we often store settings or data in JSON.
// Converting an object to a JSON string
const obj = { name: 'Anna', age: 12 };
const jsonString = JSON.stringify(obj, null, 2); // null, 2 — for pretty formatting
// Converting a JSON string back to an object
const parsed = JSON.parse(jsonString);
console.log(parsed.name); // Anna
4. Error handling (try/catch)
The program shouldn't crash if something goes wrong. Errors need to be caught.
try {
// Trying to do something risky
const data = fs.readFileSync('nonexistent_file.txt', 'utf8');
} catch (error) {
// If an error occurs — don't crash, but report it
console.log('Error: file not found. An empty list will be used.');
const data = '[]'; // Default value
}
5. Command line arguments
When we run a program, we can pass parameters to it.
// Getting arguments (be