Node.js / npm / pnpm
In diesem Artikel wird beschrieben, wie Sie die Laufzeitumgebung Node.js installieren und beliebte Paketmanager unter Ubuntu 26.04 verwenden.
Option 1: Installation über NVM (empfohlen)
Mit nvm (Node Version Manager) können Sie mehrere Node.js-Versionen installieren und zwischen ihnen wechseln. Es ist der flexibelste Ansatz:
NVM installieren
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bashLaden Sie Ihre Shell-Konfiguration neu:
source ~/.bashrcVerwenden von nvm
# List available LTS versions
nvm ls-remote --lts
# Install the latest LTS version
nvm install --lts
# Install a specific version
nvm install 24
nvm install 22
# Switch versions
nvm use 24
# Set default version
nvm alias default 24
# List installed versions
nvm ls
# Uninstall a version
nvm uninstall 20Option 2: Installation über fnm
fnm (Fast Node Manager) ist ein Rust-basierter Node.js-Versionsmanager, der schneller ist:
# Install fnm
curl -fsSL https://fnm.vercel.app/install | bash
source ~/.bashrc
# Install Node.js
fnm install --lts
fnm install 24
# Switch versions
fnm use 24
fnm default 24
# List installed versions
fnm listFügen Sie ~/.bashrc oder ~/.zshrc Unterstützung für die automatische Umschaltung hinzu:
eval "$(fnm env --use-on-cd)"Dadurch wird die Version automatisch gewechselt, wenn ein Verzeichnis mit einer .node-version-Datei eingegeben wird.
Option 3: Installation über apt
Wenn Sie nur eine einzige Version benötigen, können Sie das offizielle NodeSource-Repository verwenden:
# Add the NodeSource repository (current LTS Node.js 24 example)
curl -fsSL https://deb.nodesource.com/setup_24.x | sudo -E bash -
# Install Node.js (includes npm)
sudo apt install nodejs -y
# Verify
node --version
npm --versionnpm-Paketmanager
npm wird neben Node.js installiert und ist der Standardpaketmanager:
Grundlegende Verwendung
# Initialize a project
npm init -y
# Install a dependency
npm install express
# Install a dev dependency
npm install --save-dev typescript
# Install a tool globally
npm install -g nodemon
# Run scripts
npm run dev
npm start
# Update dependencies
npm update
npm outdatedChina Mirror konfigurieren
npm config set registry https://registry.npmmirror.comÜberprüfen Sie die Konfiguration:
npm config get registrypnpm
pnpm verwendet Hardlinks und inhaltsadressierbaren Speicher, spart Speicherplatz und ermöglicht schnellere Installationen:
Installieren Sie pnpm
# Install via npm
npm install -g pnpm
# Or standalone installation
curl -fsSL https://get.pnpm.io/install.sh | sh -Verwenden von pnpm
# Initialize a project
pnpm init
# Install dependencies
pnpm add express
pnpm add -D typescript
# Install all dependencies
pnpm install
# Run scripts
pnpm dev
pnpm start
# Update dependencies
pnpm update
# Manage global packages
pnpm add -g nodemonSpiegel konfigurieren
pnpm config set registry https://registry.npmmirror.compnpm-Arbeitsbereiche (Monorepo)
Erstellen Sie pnpm-workspace.yaml im Projektstammverzeichnis:
packages:
- 'packages/*'
- 'apps/*'# Install a dependency in a specific package
pnpm --filter @myapp/web add react
# Run a command across all packages
pnpm -r run buildGarn
Yarn ist ein weiterer beliebter Paketmanager:
Garn installieren
# Enable via corepack
# Note: as of Node.js 25, corepack is no longer bundled with official distributions and must be installed separately
# (Node.js 24 LTS and earlier still bundle corepack, so you can skip the next line)
npm install -g corepack
corepack enable
corepack prepare yarn@stable --activate
# Or install via npm
npm install -g yarnVerwendung von Garn
# Initialize a project
yarn init -2
# Install dependencies
yarn add express
yarn add --dev typescript
# Install all dependencies
yarn install
# Run scripts
yarn devKernpaket
Corepack ist ein Node.js-Tool zur Verwaltung von Paketmanagern, das sicherstellt, dass Projekte die angegebene Version von pnpm oder Yarn verwenden. Ab Node.js 25 ist es nicht mehr im Lieferumfang offizieller Distributionen enthalten und muss zuerst mit npm install -g corepack installiert werden; Node.js 24 LTS und früher bündeln es noch:
# Node.js 25+ needs corepack installed first (skip on 24 LTS and earlier)
npm install -g corepack
# Enable corepack
corepack enable
# Specify the package manager in package.json
# "packageManager": "pnpm@9.15.0"Dadurch wird sichergestellt, dass Teammitglieder nach dem Klonen des Projekts automatisch die richtige Paketmanagerversion verwenden.
FAQ
Probleme mit globalen Installationsberechtigungen
Node.js, das über NVM oder FNM installiert wird, erfordert keine Sudo-Berechtigungen. Bei der Installation über apt können Berechtigungsprobleme auftreten:
# Change the npm global install directory
mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc.nvmrc-Datei
Erstellen Sie eine .nvmrc-Datei im Projektstammverzeichnis, um die Node.js-Version anzugeben:
echo "24" > .nvmrc
nvm use # Automatically reads .nvmrcDie Wahl des richtigen Node.js-Versionsmanagers und Paketmanagers kann Ihre Frontend- und Backend-JavaScript-Entwicklungserfahrung erheblich verbessern.