Intro
Hey everyone! Lately, I've been focusing heavily on backend development, and I finally decided it was time to dive into Node.js. I'd been putting this moment off for a while, and now it's here❗
Honestly, I used to feel a bit intimidated when thinking about backend concepts-things like creating servers, working with databases...😳and general infrastructure.I wasn't sure what Node.js had in store for me...until just a few days ago.🤗
To my surprise, I genuinely love it! It's quite accessible and logical in how it operates, especially since I already have JavaScript knowledge.
There's still a vast amount to learn in the backend world, but I'm not focused on achieving perfection; I'm focused on the progress itself. The prospect of implementing these acquired skills in future projects is highly motivating, and I'm keen to move forward!
Wish me luck on this journey!😬 Hopefully I don’t break any server while learning Node.js!😂
On a serious note, here’s what we’ll cover in detail in this first section:
1. Setting the Stage: Installation, REPL, and package.json
2. Modularity: Understanding Modules (CommonJS vs. ES Modules)
3. The Workflow: NPM Scripts, Nodemon, and .gitignore
4. Security Baseline: Environment Variables and the .env
file
What is Node.js?
Node.js is a JavaScript runtime built on Chrome's V8 engine that allows you to run JavaScript on the server side. Unlike traditional JavaScript, which runs in the browser, Node.js enables you to build server-side applications, manage files and databases, create APIs, and much more using the same language.
Node.js is commonly used for server-side and back-end development, including:
➤ Web servers and real-time applications,
➤ REST or GraphQL APIs for communication between front-end and back-end,
➤ Database management (MongoDB, PostgreSQL, MySQL, etc.)
➤ Automation and server-side scripts,
➤ Microservices and scalable applications, thanks to its asynchronous, event-driven architecture.
1. Setting the Stage
Before we write a single line of server code, we need to ensure we have the runtime environment installed and that our project is properly initialized!
1.a) Installation:
The very first step is, obviously, getting Node.js into your system.
These commands should display the installed versions, confirming that both the Node.js engine and NPM (Node Package Manager) are ready to go.
node -v
npm -v
1.b) Node REPL (The Instant Playground):
REPL (Read-Eval-Print Loop) is atool for quickly testing a function, new syntax, or a native module without writing and saving an entire file.
test cod:
node
> console.log("Hello, Node.js!")
1.c) package.json:
The package.json
is arguably the most crucial file in a Node.js project. It is the identity card of your application.
To create this file, navigate to your project directory and run:
npm init -y
So far, it feels like a simple, familiar cake, right?🤗
2. Modularity
A key component of any robust Node.js project is how code is organized and shared across separate files. Node.js currently supports two major module systems. Understanding the difference is crucial for you!
2.a) CommonJS (CJS) - The Classic
This was the original and long-standing standard system in Node.js.
Syntax: Uses the require()
function and module.exports
.
Execution: Module loading is synchronous (it stops execution until the module is fully loaded).
Usage: It's the default for files with the .js
extension unless otherwise specified.
example:
// exporting module
module.exports = {
add: (a, b) => a + b
};
// importing module
const { add } = require('./math');
console.log(add(5, 5));
2.b) ES Modules (ESM) - The Modern Standard
ESM is the official JavaScript standard (introduced in ES6) and is now fully supported and recommended in Node.js for new projects.
Syntax: Uses the import
and export keywords
.
Execution: Supports asynchronous loading and is generally more efficient.
Usage: Requires either the .mjs
extension or setting "type": "module"
in your package.json file
.
// exporting module
export const subtract = (a, b) => a - b;
// importing module
import { subtract } from './math.js';
console.log(subtract(10, 3));
I suggest you be careful when working with modules, ES Modules are the modern standard, but make sure you know what type of module your project uses before importing anything. I actually ran into this myself at first❗🙃
3. The Workflow
These functionalities will help you automate and streamline your workflow, saving time and avoiding repetitive tasks.
3.a) NPM Scripts
Automate terminal commands directly from your package.json
.
For example, you can create a script to start the server:
// package.json
"scripts": {
"start": "node server.js",
"dev": "nodemon server.js"
}
Then, you run them easily with: npm run start
or npm run dev
.
3.b) Nodemon(The Automatic Refresher)
Nodemon is an essential development-only NPM package. Why is it crucial❓It automatically monitors changes in your source files and restarts your Node.js server every time you save.
Installation (as a dev dependency): npm install -D nodemon
3.c) .gitignore (Keeping GitHub Clean)
This file is vital for maintaining a clean and secure repository. It instructs Git which files and directories to ignore during a commit.
Must-Ignore: The two most critical items to ignore are the node_modules
directory (huge!) and the .env
file (sensitive data!).
4. Security Baseline
We will have sensitive information that should never be committed to source control (like GitHub). This includes API keys, database passwords, and specific configuration settings for different environments.This is where Environment Variables come in, example of .env
page:
DB_HOST=localhost
DB_PORT=5432
API_KEY=my_super_secret_key_12345
Verry important! Remember to always include the .env
file in your .gitignore
❗ This ensures your secrets stay local and secure.
Conclusion❤️
So far, everything seems straightforward! Of corse, I'll continue in the next post with a deep dive into how to create your first own server.
I'm committed to posting my learnings here with all of you, both for those who want a recap and for myself-as they say, you learn fastest by explaining to someone else.
I'm genuinely excited to have stepped into the backend world. It feels like a new beginning and has opened up new paths that really spark my curiosity. If you're reading this, you're either already familiar with Node.js or you're planning to learn it, so be proud of where you are right now. You're doing great, and you're exactly where you need to be! Keep coding!
I highly recommend you check the documentation as well HERE.
Don't overlook the official documentation! Tools and quick searches are perfect for immediate needs, but the documentation is where you'll find the complete, official context necessary for learning more about Node.js.
And if you enjoyed this post, please leave a comment or a reaction❤️! I truly appreciate the gesture and the fact that you took the time to read my work.
Good luck and happy coding!🤗
Top comments (0)