Node.js

Runtime JavaScript pour le développement serveur - applications réseau évolutives avec I/O non bloquant

TL;DR

Quoi : Runtime JavaScript construit sur le moteur V8 de Chrome pour le développement côté serveur.

Pourquoi : Exécuter JavaScript en dehors du navigateur, construire des applications réseau rapides et évolutives.

Quick Start

Installation (recommandée via nvm) :

# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash

# Install latest LTS
nvm install --lts

# Verify installation
node -v  # v24.12.0
npm -v

Installations alternatives :

macOS : brew install node

Windows : choco install nodejs ou télécharger depuis nodejs.org

Premier programme :

// hello.js
console.log('Hello, Node.js!');
node hello.js

Créer un serveur web :

// server.js
const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});
node server.js

Cheatsheet

CommandeDescription
node file.jsExécuter un fichier JavaScript
node -e "code"Exécuter du code inline
node --versionAfficher la version Node.js
npm init -yInitialiser un nouveau projet
npm install pkgInstaller un package
npm install -g pkgInstaller globalement
npm run scriptExécuter un script package.json
npx commandExécuter un package sans l’installer

Gotchas

’node’ command not found after install

# Reload shell config
source ~/.bashrc  # or ~/.zshrc
# Or restart terminal

EACCES permission errors with npm

# Use nvm instead of system install
# Or fix npm permissions:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
export PATH=~/.npm-global/bin:$PATH

Callback hell

// Use async/await instead
async function getData() {
  const result = await fetch('https://api.example.com');
  return result.json();
}

ES Modules vs CommonJS

// CommonJS (default)
const fs = require('fs');

// ES Modules (use .mjs or set "type": "module" in package.json)
import fs from 'fs';

Next Steps