Node.js

JavaScript-Runtime für serverseitige Entwicklung - skalierbare Netzwerkanwendungen mit non-blocking I/O

TL;DR

Was: JavaScript-Runtime basierend auf Chromes V8-Engine für serverseitige Entwicklung.

Warum: JavaScript außerhalb des Browsers ausführen, schnelle und skalierbare Netzwerkanwendungen bauen.

Quick Start

Installation (empfohlen 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

Alternative Installationen:

macOS: brew install node

Windows: choco install nodejs oder von nodejs.org herunterladen

Erstes Programm:

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

Webserver erstellen:

// 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

BefehlBeschreibung
node file.jsJavaScript-Datei ausführen
node -e "code"Inline-Code ausführen
node --versionNode.js-Version anzeigen
npm init -yNeues Projekt initialisieren
npm install pkgPaket installieren
npm install -g pkgGlobal installieren
npm run scriptpackage.json-Script ausführen
npx commandPaket ohne Installation ausführen

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