Zum Inhalt

npm Paketmanager

Umfassende npm (Node Package Manager) Befehle und Workflows für JavaScript und Node.js Entwicklung.

Installation und Inbetriebnahme

Install Node.js und npm

```bash

Using Node Version Manager (recommended)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh|bash nvm install node nvm use node

Direct download from nodejs.org

Or using package managers:

macOS: brew install node

Ubuntu: sudo apt install nodejs npm

Windows: choco install nodejs

```_

Basispaket-Operationen_TABLE_92_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Projektmanagement

Projekt Initialisierung

Command Description
INLINE_CODE_15 Initialize new project
INLINE_CODE_16 Initialize with defaults
INLINE_CODE_17 Initialize with scoped template
INLINE_CODE_18 Create project with template

Abhängigkeitsmanagement

Command Description
INLINE_CODE_19 Install all dependencies
INLINE_CODE_20 Install and save to dependencies
INLINE_CODE_21 Install and save to devDependencies
INLINE_CODE_22 Install as optional dependency
INLINE_CODE_23 Install without saving

Paketinformationen

Command Description
INLINE_CODE_24 Search for packages
INLINE_CODE_25 Show package information
INLINE_CODE_26 View package details
INLINE_CODE_27 Show outdated packages
INLINE_CODE_28 Check for vulnerabilities
INLINE_CODE_29 Fix vulnerabilities
_
Scripts und Ausführung

Script Management

Command Description
INLINE_CODE_30 Run custom script
INLINE_CODE_31 Run start script
INLINE_CODE_32 Run test script
INLINE_CODE_33 Run build script
INLINE_CODE_34 Run development script

Beispielpaket.json Scripts

json \\\\{ "scripts": \\\\{ "start": "node server.js", "dev": "nodemon server.js", "build": "webpack --mode production", "test": "jest", "lint": "eslint src/", "format": "prettier --write src/" \\\\} \\\\}_

/ Version Management

Semantische Version

Command Description
INLINE_CODE_35 Increment patch version
INLINE_CODE_36 Increment minor version
INLINE_CODE_37 Increment major version
INLINE_CODE_38 Create prerelease version

Paketinstallation durch Version

Command Description
INLINE_CODE_39 Install specific version
INLINE_CODE_40 Install latest version
INLINE_CODE_41 Install next/beta version
INLINE_CODE_42 Install compatible version

Konfiguration

npm Configuration

Command Description
INLINE_CODE_43 Show configuration
INLINE_CODE_44 Get configuration value
INLINE_CODE_45 Set configuration value
INLINE_CODE_46 Delete configuration

Gemeinsame Konfiguration

Setting Description
INLINE_CODE_47 Set registry
INLINE_CODE_48 Set default author
INLINE_CODE_49 Set default license
INLINE_CODE_50 Save exact versions
_
### .npmrc Datei
```ini
# Project .npmrc
registry=https://registry.npmjs.org/
save-exact=true
engine-strict=true
fund=false
audit-level=moderate

Global .npmrc (~/.npmrc)

init-author-name=Your Name init-author-email=your.email@example.com init-license=MIT ```_

Veröffentlichung

Paket Publishing

Command Description
INLINE_CODE_51 Login to npm registry
INLINE_CODE_52 Check logged in user
INLINE_CODE_53 Publish package
INLINE_CODE_54 Publish scoped package publicly
INLINE_CODE_55 Unpublish specific version
_
### Publishing Workflow
```bash
# 1. Update version
npm version patch

2. Build package

npm run build

3. Test package

npm test

4. Publish

npm publish

5. Tag release

git tag v1.0.0 git push origin v1.0.0 ```_

/ Fortgeschrittene Nutzung

Workspaces (npm 7+)

json \\\\{ "name": "my-monorepo", "workspaces": [ "packages/*", "apps/*" ] \\\\}_

Command Description
INLINE_CODE_56 Install all workspace dependencies
INLINE_CODE_57 Run tests in all workspaces
INLINE_CODE_58 Run command in specific workspace

Paket Linking

Command Description
INLINE_CODE_59 Create global link
INLINE_CODE_60 Link to global package
INLINE_CODE_61 Unlink package

Cache Management

Command Description
INLINE_CODE_62 Verify cache integrity
INLINE_CODE_63 Clear cache
INLINE_CODE_64 List cached packages

Sicherheit

Security Auditing

Command Description
INLINE_CODE_65 Check for vulnerabilities
INLINE_CODE_66 Check high severity only
INLINE_CODE_67 Fix vulnerabilities automatically
INLINE_CODE_68 Force fix (may break changes)

Paket Verification

Command Description
INLINE_CODE_69 Create tarball
INLINE_CODE_70 Show what would be packed
INLINE_CODE_71 Install from tarball

 Leistungsoptimierung

Faster Installation

```bash

Use npm ci for CI/CD

npm ci

Parallel installation

npm install --prefer-offline

Skip optional dependencies

npm install --no-optional

Production only

npm install --production ```_

Alternative Paketmanager

Manager Installation Benefits
Yarn INLINE_CODE_72 Faster, deterministic
pnpm INLINE_CODE_73 Disk space efficient
Bun INLINE_CODE_74 Extremely fast

Fehlerbehebung

Gemeinsame Themen

Problem Solution
Permission errors Use nvm or fix permissions
Package conflicts Delete node_modules and reinstall
Outdated npm INLINE_CODE_75
Registry issues INLINE_CODE_76

Debugging

Command Description
INLINE_CODE_77 Check npm environment
INLINE_CODE_78 Check dependency tree
INLINE_CODE_79 Show top-level dependencies
INLINE_CODE_80 Show why package is installed

Reset and Clean Install

```bash

Clean install

rm -rf node_modules package-lock.json npm install

Clear npm cache

npm cache clean --force

Reset npm configuration

npm config edit ```_

oder Best Practices

Paket.json Management

json \\\\{ "name": "my-project", "version": "1.0.0", "description": "Project description", "main": "index.js", "engines": \\\\{ "node": ">=16.0.0", "npm": ">=8.0.0" \\\\}, "dependencies": \\\\{ "express": "^4.18.0" \\\\}, "devDependencies": \\\\{ "jest": "^29.0.0", "eslint": "^8.0.0" \\\\}, "peerDependencies": \\\\{ "react": ">=16.0.0" \\\\} \\\\}_

Development Workflow

ANHANG Version Pinning*: Verwenden Sie genaue Versionen für kritische Abhängigkeiten 2. **Lock Files: Paket-lock.json zur Versionskontrolle 3. Sicherheit: Regelmäßige Sicherheitsaudits mit npm audit_ 4. Test: Test vor der Veröffentlichung npm pack 5. Dokumentation*: Vollständiges LESEN

Performance

ANHANG CI/CD: Verwendung npm ci in der kontinuierlichen Integration 2. Caching: Leverage npm cache in CI-Umgebungen 3. Selective Installation*: Verwendung --production für Produktionsgebäude 4. **Alternative Manager*: Berücksichtigen Sie Garn oder Pnpm für große Projekte 5. **Registry: Private Registrierung für interne Pakete verwenden