Node.js

Runtime JavaScript para desarrollo del servidor - aplicaciones de red escalables con I/O no bloqueante

TL;DR

Qué: Runtime de JavaScript construido sobre el motor V8 de Chrome para desarrollo del lado del servidor.

Por qué: Ejecutar JavaScript fuera del navegador, construir aplicaciones de red rápidas y escalables.

Quick Start

Instalación (recomendado 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

Instalaciones alternativas:

macOS: brew install node

Windows: choco install nodejs o descargar de nodejs.org

Primer programa:

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

Crear un servidor 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

ComandoDescripción
node file.jsEjecutar archivo JavaScript
node -e "code"Ejecutar código inline
node --versionMostrar versión de Node.js
npm init -yInicializar nuevo proyecto
npm install pkgInstalar paquete
npm install -g pkgInstalar globalmente
npm run scriptEjecutar script de package.json
npx commandEjecutar paquete sin instalar

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