Débutant15 min de lecture

Introduction to Node.js

Discover what Node.js is, how it differs from browser JavaScript, and set up your development environment.

What is Node.js?

Up until now, every piece of JavaScript you have written has run inside a web browser. The browser has a built-in JavaScript engine — Chrome uses V8, Firefox uses SpiderMonkey, Safari uses JavaScriptCore — that reads and executes your code. But JavaScript was trapped there. It could not read files on your computer, could not start a web server, and could not interact with databases. It was a browser-only language.

In 2009, Ryan Dahl changed everything by creating Node.js. He took Chrome's V8 JavaScript engine, pulled it out of the browser, and wrapped it in a program that could run on any computer — servers, laptops, Raspberry Pis, anywhere. He added capabilities that browsers do not have: reading and writing files, creating network connections, starting HTTP servers, accessing the operating system.

This was revolutionary. For the first time, JavaScript could run outside the browser. Web developers who already knew JavaScript could now write server-side code without learning a completely new language like Python, Java, or Ruby.

Node.js is NOT a framework. It is not a library. It is a runtime environment — a program that executes JavaScript code. Just like your browser is a runtime for frontend JavaScript, Node.js is a runtime for backend JavaScript. Think of it as a translator: it takes your JavaScript code and translates it into instructions your operating system can understand.

Today, Node.js is one of the most popular backend technologies in the world. It powers the backends of Netflix, PayPal, LinkedIn, NASA, Trello, and millions of other applications.

Why Node.js?

Node.js became massively popular for several compelling reasons:

JavaScript Everywhere — The biggest advantage is using the same language on both frontend and backend. If you know JavaScript, you already know the syntax for Node.js. Your team can share code between the browser and the server. A single developer can build the entire stack. This is why Node.js is often called the backbone of full-stack JavaScript.

Non-Blocking I/O — Node.js uses an event-driven, non-blocking model. In simple terms: when Node.js needs to do something slow (like reading a file or querying a database), it does not wait around doing nothing. Instead, it moves on to handle other requests and comes back when the slow operation finishes. Imagine a chef who puts pasta in boiling water and, instead of staring at the pot, starts preparing the sauce. This makes Node.js exceptionally efficient at handling many simultaneous connections — perfect for APIs and real-time applications.

Massive Ecosystem (npm) — Node.js comes with npm (Node Package Manager), the largest software registry in the world with over 2 million packages. Need to send emails? There is a package for that. Need to process images? There is a package. Authentication, database drivers, PDF generation, web scraping — someone has already built and shared a solution.

Real-World Adoption — Netflix migrated to Node.js and reduced their startup time by 70%. PayPal rebuilt their account overview page in Node.js and saw a 35% decrease in response time. LinkedIn moved from Ruby to Node.js and went from 30 servers to 3. These are not small companies experimenting — they are billion-dollar businesses choosing Node.js for performance and developer productivity.

Node.js excels at building REST APIs, real-time applications (chat, gaming, live dashboards), microservices, CLI tools, and serverless functions.

Installing Node.js

Setting up Node.js is straightforward. Here is how to get started:

Step 1: Download Node.js Go to nodejs.org. You will see two versions: LTS (Long-Term Support) and Current. Always choose LTS for learning and production work — it is the stable, well-tested version that receives security updates for years.

Step 2: Install Run the installer and follow the prompts. On macOS and Windows, it is a standard installation wizard. On Linux, you can use your package manager (apt, yum) or download the binary.

Step 3: Verify the installation Open your terminal (Command Prompt on Windows, Terminal on macOS/Linux) and run these commands:

bash
node --version    # Should print something like v20.11.0
npm --version     # Should print something like 10.2.4

If both commands print version numbers, you are ready to go.

What gets installed:

  • node — The JavaScript runtime. You use this to execute .js files: node myfile.js
  • npm — The package manager. You use this to install third-party libraries: npm install express
  • npx — The package runner. You use this to run packages without installing them globally: npx create-react-app my-app

Pro tip: Many professional developers use a version manager like nvm (Node Version Manager) instead of installing Node.js directly. This lets you switch between different Node.js versions for different projects. Install it from github.com/nvm-sh/nvm.

Node.js vs Browser JavaScript

html
<div style="font-family:sans-serif; padding:16px;">
  <h3>Node.js vs Browser JavaScript</h3>
  <div id="comparison"></div>
</div>

<script>
  const differences = [
    {
      feature: "DOM Access",
      browser: "document, window, alert()",
      node: "Not available (no browser UI)"
    },
    {
      feature: "File System",
      browser: "Not available (security sandbox)",
      node: "fs module — read/write files"
    },
    {
      feature: "HTTP Server",
      browser: "Can only send requests",
      node: "Can create servers with http module"
    },
    {
      feature: "Global Object",
      browser: "window",
      node: "global (or globalThis)"
    },
    {
      feature: "Modules",
      browser: "ES Modules (import/export)",
      node: "CommonJS (require) + ES Modules"
    },
    {
      feature: "Process Info",
      browser: "navigator object",
      node: "process object (env, argv, exit)"
    }
  ];

  let html = '<table style="border-collapse:collapse; width:100%; font-size:14px;">';
  html += '<tr style="background:#1e293b; color:white;">';
  html += '<th style="padding:8px; border:1px solid #334155;">Feature</th>';
  html += '<th style="padding:8px; border:1px solid #334155;">Browser JS</th>';
  html += '<th style="padding:8px; border:1px solid #334155;">Node.js</th></tr>';

  differences.forEach(function(diff, i) {
    const bg = i % 2 === 0 ? "#f8fafc" : "white";
    html += '<tr style="background:' + bg + ';">';
    html += '<td style="padding:8px; border:1px solid #e2e8f0; font-weight:bold;">' + diff.feature + '</td>';
    html += '<td style="padding:8px; border:1px solid #e2e8f0;">' + diff.browser + '</td>';
    html += '<td style="padding:8px; border:1px solid #e2e8f0;">' + diff.node + '</td></tr>';
  });
  html += '</table>';

  document.getElementById("comparison").innerHTML = html;
</script>

Node.js vs Browser JavaScript

Although Node.js and browser JavaScript use the same language, they live in very different environments. Understanding what is available in each context is crucial.

What Node.js DOES NOT have (that browsers do):

  • No document object — there is no HTML page to manipulate
  • No window object — there is no browser window
  • No alert(), confirm(), or prompt() — there is no UI
  • No localStorage or sessionStorage — those are browser-only storage
  • No fetch() (in older versions) — though modern Node.js 18+ includes it

What Node.js HAS (that browsers do not):

  • fs module — read and write files on the computer's hard drive
  • http / https modules — create web servers and make HTTP requests
  • path module — work with file system paths
  • process object — access environment variables, command-line arguments, exit the program
  • os module — get operating system information (CPU, memory, hostname)
  • crypto module — encryption, hashing, secure random numbers
  • Direct database access — connect to PostgreSQL, MongoDB, MySQL, etc.

What BOTH share:

  • Same JavaScript syntax (variables, functions, loops, classes, promises)
  • Same data types (strings, numbers, booleans, arrays, objects)
  • Same built-in objects (Math, Date, JSON, Map, Set, Promise)
  • Same error handling (try/catch, throw)
  • console.log() works in both (prints to browser console or terminal)

The key takeaway: Node.js trades browser capabilities (DOM, window) for system capabilities (files, network, OS). It is the same language wearing a different hat.

What is Node.js?

Prêt à pratiquer ?

Crée ton compte gratuit pour accéder à l'éditeur de code interactif, lancer les défis et suivre ta progression.