npm Gestionnaire de paquets
Copier toutes les commandes
Générer PDF
Commandes et workflows complets npm (Node Package Manager) pour le développement JavaScript et Node.js.
Installation et configuration
Installer Node.js et npm
# 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
Opérations de base
Command |
Description |
npm install package_name |
Install package locally |
npm install -g package_name |
Install package globally |
npm uninstall package_name |
Uninstall package |
npm update |
Update all packages |
npm update package_name |
Update specific package |
npm list |
List installed packages |
npm list -g |
List global packages |
Gestion de projet
Initialisation du projet
Command |
Description |
npm init |
Initialize new project |
npm init -y |
Initialize with defaults |
npm init @scope |
Initialize with scoped template |
npm create package_name |
Create project with template |
Gestion de la dépendance
Command |
Description |
npm install |
Install all dependencies |
npm install --save package |
Install and save to dependencies |
npm install --save-dev package |
Install and save to devDependencies |
npm install --save-optional package |
Install as optional dependency |
npm install --no-save package |
Install without saving |
Command |
Description |
npm search keyword |
Search for packages |
npm info package_name |
Show package information |
npm view package_name |
View package details |
npm outdated |
Show outdated packages |
npm audit |
Check for vulnerabilities |
npm audit fix |
Fix vulnerabilities |
Scripts et exécution
Gestion des scripts
Command |
Description |
npm run script_name |
Run custom script |
npm start |
Run start script |
npm test |
Run test script |
npm run build |
Run build script |
npm run dev |
Run development script |
Exemple paquet.json Scripts
\\\\{
"scripts": \\\\{
"start": "node server.js",
"dev": "nodemon server.js",
"build": "webpack --mode production",
"test": "jest",
"lint": "eslint src/",
"format": "prettier --write src/"
\\\\}
\\\\}
```_
## Gestion des versions
### Version sémantique
|Command|Description|
|---------|-------------|
|`npm version patch`|Increment patch version|
|`npm version minor`|Increment minor version|
|`npm version major`|Increment major version|
|`npm version prerelease`|Create prerelease version|
### Installation du paquet par version
|Command|Description|
|---------|-------------|
|`npm install package@1.2.3`|Install specific version|
|`npm install package@latest`|Install latest version|
|`npm install package@next`|Install next/beta version|
|`npm install package@^1.2.0`|Install compatible version|
## Configuration
### Configuration de npm
|Command|Description|
|---------|-------------|
|`npm config list`|Show configuration|
|`npm config get key`|Get configuration value|
|`npm config set key value`|Set configuration value|
|`npm config delete key`|Delete configuration|
### Configuration commune
|Setting|Description|
|---------|-------------|
|`npm config set registry https://registry.npmjs.org/`|Set registry|
|`npm config set init-author-name "Your Name"`|Set default author|
|`npm config set init-license "MIT"`|Set default license|
|`npm config set save-exact true`|Save exact versions|
### Fichier .npmrc
```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
```_
## Édition
### Édition de paquets
|Command|Description|
|---------|-------------|
|`npm login`|Login to npm registry|
|`npm whoami`|Check logged in user|
|`npm publish`|Publish package|
|`npm publish --access public`|Publish scoped package publicly|
|`npm unpublish package@version`|Unpublish specific version|
### Flux de travail de l'édition
```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
Utilisation avancée
Espaces de travail (npm 7+)
\\\\{
"name": "my-monorepo",
"workspaces": [
"packages/*",
"apps/*"
]
\\\\}
Command |
Description |
npm install --workspaces |
Install all workspace dependencies |
npm run test --workspaces |
Run tests in all workspaces |
npm run build --workspace=package-a |
Run command in specific workspace |
Lien entre les paquets
Command |
Description |
npm link |
Create global link |
npm link package_name |
Link to global package |
npm unlink package_name |
Unlink package |
Gestion des caches
Command |
Description |
npm cache verify |
Verify cache integrity |
npm cache clean --force |
Clear cache |
npm cache ls |
List cached packages |
Sécurité
Vérification de la sécurité
Command |
Description |
npm audit |
Check for vulnerabilities |
npm audit --audit-level high |
Check high severity only |
npm audit fix |
Fix vulnerabilities automatically |
npm audit fix --force |
Force fix (may break changes) |
Vérification de l'emballage
Command |
Description |
npm pack |
Create tarball |
npm pack --dry-run |
Show what would be packed |
npm install package.tgz |
Install from tarball |
Installation plus rapide
# 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
Autres gestionnaires de paquets
Manager |
Installation |
Benefits |
Yarn |
npm install -g yarn |
Faster, deterministic |
pnpm |
npm install -g pnpm |
Disk space efficient |
Bun |
curl -fsSL https://bun.sh/install \|bash |
Extremely fast |
Dépannage
Questions communes
Problem |
Solution |
Permission errors |
Use nvm or fix permissions |
Package conflicts |
Delete node_modules and reinstall |
Outdated npm |
npm install -g npm@latest |
Registry issues |
npm config set registry https://registry.npmjs.org/ |
Déboguement
Command |
Description |
npm doctor |
Check npm environment |
npm ls |
Check dependency tree |
npm ls --depth=0 |
Show top-level dependencies |
npm why package_name |
Show why package is installed |
Réinitialiser et nettoyer l'installation
# Clean install
rm -rf node_modules package-lock.json
npm install
# Clear npm cache
npm cache clean --force
# Reset npm configuration
npm config edit
Meilleures pratiques
Je vous en prie. Gestion
\\\\{
"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"
\\\\}
\\\\}
Développement Flux de travail
- ** Pinning de la version**: Utiliser des versions exactes pour les dépendances critiques
- Lock Files: Commit package-lock.json au contrôle de version
- Sécurité: Audits de sécurité réguliers avec
npm audit
- Test: Test avant publication avec
npm pack
- Documentation: Maintenir un README complet
Rendement
- CI/CD: Utiliser
npm ci
dans l'intégration continue
- Cachage: Mettre à profit le cache npm dans les environnements CI
- Installation sélective: Utiliser
--production
pour les constructions de production
- ** Directeurs alternatifs**: Considérer le fil ou le pnpm pour les grands projets
- Registre: Utiliser un registre privé pour les paquets internes