npm Administrador de paquetes
"Clase de la hoja"
########################################################################################################################################################################################################################################################## Copiar todos los comandos
■/div titulada
Instrucciones y flujos de trabajo para el desarrollo de JavaScript y Node.js.
Instalación
Instala Node.js y 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
Operaciones básicas de paquetes
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 |
Project Management
Inicialización del proyecto
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 |
Administración de dependencias
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 |
Guiones y Ejecución
Script Management
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 |
Ejemplo paquete.json Scripts
\\\\{
"scripts": \\\\{
"start": "node server.js",
"dev": "nodemon server.js",
"build": "webpack --mode production",
"test": "jest",
"lint": "eslint src/",
"format": "prettier --write src/"
\\\\}
\\\\}
Administración de versiones
Versión semántica
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 |
Instalación de paquetes por versión
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 |
Configuración
npm Configuración
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 |
Configuración común
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 |
.npmrc File
# 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
Publicación
Paquete Publishing
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 |
Publishing Workflow
# 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
Uso avanzado
Espacios de trabajo (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 |
Enlace del paquete
Command |
Description |
npm link |
Create global link |
npm link package_name |
Link to global package |
npm unlink package_name |
Unlink package |
Cache Management
Command |
Description |
npm cache verify |
Verify cache integrity |
npm cache clean --force |
Clear cache |
npm cache ls |
List cached packages |
Seguridad
Auditoría de la seguridad
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) |
Verificación de paquetes
Command |
Description |
npm pack |
Create tarball |
npm pack --dry-run |
Show what would be packed |
npm install package.tgz |
Install from tarball |
Optimización del rendimiento
Instalación más rápida
# 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
Administradores de paquetes alternativos
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 |
Solución de problemas
Cuestiones comunes
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/ |
Debugging
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 |
Reiniciar e instalar limpio
# Clean install
rm -rf node_modules package-lock.json
npm install
# Clear npm cache
npm cache clean --force
# Reset npm configuration
npm config edit
Buenas prácticas
Paquete.json Gestión
\\\\{
"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
- Version Pinning: Utilizar versiones exactas para dependencias críticas
- Lock Files: Commit package-lock.json to version control
- Seguridad: Auditorías periódicas de seguridad con
npm audit
- Testing: Test before publishing with
npm pack
- Documentación: Mantener una README integral
Ejecución
- CI/CD: Uso
npm ci
en integración continua
- Caching: Leverage npm cache in CI environments
- ** Instalación selectiva**: Uso
--production
para la producción
- ** Gestores alternativos**: Considerar hilo o pnpm para grandes proyectos
- Registry: Use registro privado para paquetes internos