Modern applications depend on packages. Composer manages PHP dependencies, npm handles JavaScript. Understanding both enables efficient full-stack development. At ZIRA Software, proper package management prevents version conflicts and security issues.
Purpose and Ecosystem
Composer (PHP):
- PHP dependency manager
- Packagist.org registry (300K+ packages)
- Project-level dependencies
- Autoloading via PSR-4
npm (JavaScript):
- JavaScript package manager
- npmjs.com registry (1M+ packages)
- Project and global packages
- Module resolution via require/import
Installation
Composer:
# macOS/Linux
curl -sS https://getcomposer.org/installer | php
sudo mv composer.phar /usr/local/bin/composer
# Verify
composer --version
npm:
# Comes with Node.js
# macOS
brew install node
# Verify
npm --version
node --version
Project Initialization
Composer:
composer init
# Creates composer.json
# Typical composer.json
{
"name": "zirasoftware/my-app",
"description": "My Laravel application",
"require": {
"php": "^7.2",
"laravel/framework": "^5.8"
},
"require-dev": {
"phpunit/phpunit": "^8.0"
},
"autoload": {
"psr-4": {
"App\\": "app/"
}
}
}
npm:
npm init
# Creates package.json
# Typical package.json
{
"name": "my-app",
"version": "1.0.0",
"description": "My React application",
"dependencies": {
"react": "^16.8.0",
"react-dom": "^16.8.0"
},
"devDependencies": {
"webpack": "^4.0.0",
"babel-loader": "^8.0.0"
},
"scripts": {
"build": "webpack",
"dev": "webpack --watch"
}
}
Installing Packages
Composer:
# Install single package
composer require guzzlehttp/guzzle
# Install dev dependency
composer require --dev phpunit/phpunit
# Install from composer.json
composer install
# Update packages
composer update
# Update specific package
composer update guzzlehttp/guzzle
npm:
# Install single package
npm install axios
# Install dev dependency
npm install --save-dev webpack
# Install from package.json
npm install
# Update packages
npm update
# Update specific package
npm update axios
# Global installation
npm install -g @vue/cli
Semantic Versioning
Both use semver (MAJOR.MINOR.PATCH):
Version constraints:
// Exact version
"package": "1.2.3"
// Greater than or equal
"package": ">=1.2.3"
// Tilde (patch updates)
"package": "~1.2.3" // 1.2.3 <= version < 1.3.0
// Caret (minor updates)
"package": "^1.2.3" // 1.2.3 <= version < 2.0.0
Lock Files
Composer (composer.lock):
- Locks exact versions
- Commit to version control
- Ensures consistent environments
- Generated by
composer install
npm (package-lock.json):
- Locks dependency tree
- Commit to version control
- Faster, reproducible installs
- Generated automatically
Usage:
# Development (first time)
composer install # Creates lock file
npm install # Creates lock file
# Production deployment
composer install --no-dev --optimize-autoloader
npm ci # Uses lock file, faster than npm install
Autoloading vs Module Resolution
Composer autoloading:
// composer.json
{
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\": "database/"
},
"files": [
"app/helpers.php"
]
}
}
// Regenerate autoloader
composer dump-autoload
// Usage
use App\Models\User;
$user = new User();
npm module resolution:
// CommonJS (Node.js)
const axios = require('axios');
// ES6 modules (modern JavaScript)
import axios from 'axios';
import { useState } from 'react';
Scripts
Composer scripts:
{
"scripts": {
"test": "phpunit",
"post-install-cmd": [
"php artisan cache:clear",
"php artisan config:clear"
],
"post-update-cmd": [
"php artisan optimize"
]
}
}
composer test
npm scripts:
{
"scripts": {
"dev": "webpack --mode development --watch",
"build": "webpack --mode production",
"test": "jest",
"lint": "eslint src/",
"start": "node server.js"
}
}
npm run dev
npm run build
npm test # Shorthand for npm run test
Security
Composer security:
# Check for known vulnerabilities
composer audit
# Update vulnerable packages
composer update --with-dependencies
npm security:
# Check for vulnerabilities
npm audit
# Fix automatically
npm audit fix
# Force fix breaking changes
npm audit fix --force
Private Packages
Composer (private repository):
{
"repositories": [
{
"type": "vcs",
"url": "https://github.com/zirasoftware/private-package"
}
],
"require": {
"zirasoftware/private-package": "^1.0"
}
}
npm (private registry):
# Login to private registry
npm login --registry=https://npm.zirasoftware.com
# Install private package
npm install @zirasoftware/private-package
Performance Comparison
Composer:
- Class autoloading via optimized autoloader
- Platform-specific extensions (APCu, OPcache)
- Parallel downloads (Composer 2.0+)
npm:
- Faster with
npm ciin CI/CD - Alternative:
yarn(parallel installs) - Alternative:
pnpm(disk-efficient)
Best Practices
Composer:
# Production optimization
composer install --no-dev --optimize-autoloader --classmap-authoritative
# Keep composer.lock in version control
git add composer.lock
npm:
# Use lock file in CI
npm ci
# Clean install
rm -rf node_modules package-lock.json
npm install
# Keep package-lock.json in version control
git add package-lock.json
Conclusion
Composer and npm serve different ecosystems with similar principles. Understanding both enables efficient full-stack development. Lock files ensure consistency, security audits prevent vulnerabilities.
Need help with PHP or JavaScript project setup? Contact ZIRA Software for development consultation.