prolificjones / npm-plugin-installer
Installs npm packages and patches package.json after composer install/update.
Package info
github.com/prolificjones82/npm-plugin-installer
Type:composer-plugin
pkg:composer/prolificjones/npm-plugin-installer
Requires
- php: >=8.1
- composer-plugin-api: ^2.0
Requires (Dev)
- composer/composer: ^2.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
A Composer plugin that automatically installs npm packages and patches package.json during Composer install/update operations.
Features
- Automatically creates
package.jsonif it doesn't exist - Installs npm/yarn packages configured in
composer.json - Automatically updates
package.jsonwith custom configuration - Supports both npm and yarn package managers
- Separates production and development dependencies
- Runs after every Composer install/update operation
Requirements
- PHP >= 8.1
- Composer >= 2.0
- Node.js and npm (or yarn)
Installation
Install the plugin via Composer:
composer require prolificjones/npm-plugin-installer
The plugin will be activated automatically when added to your project.
Configuration
Configure the plugin in your composer.json file under the extra section:
{
"extra": {
"npm-installer": {
"manager": "npm",
"packages": {
"package-name": "^1.0.0",
"another-package": "latest"
},
"dev-packages": {
"dev-tool": "^2.0.0"
},
"package-json": {
"scripts": {
"build": "webpack",
"dev": "webpack --watch"
},
"engines": {
"node": ">=16.0.0"
}
}
}
}
}
Configuration Options
- manager (string, default:
npm): The package manager to use. Options:npmoryarn - packages (object|array): Production dependencies to install
- As object:
{ "package-name": "version" } - As array:
["package-name", "another-package"]
- As object:
- dev-packages (object|array): Development dependencies to install (uses
--save-devor--dev)- As object:
{ "package-name": "version" } - As array:
["package-name"]
- As object:
- package-json (object): Custom configuration to merge into
package.json(e.g., scripts, engines, keywords)
Usage
Basic Setup
Add packages to install in your composer.json:
{
"extra": {
"npm-installer": {
"packages": ["lodash", "axios"],
"dev-packages": ["webpack", "webpack-cli"]
}
}
}
Then run:
composer install
With Custom Package Manager
To use yarn instead of npm:
{
"extra": {
"npm-installer": {
"manager": "yarn",
"packages": {
"vue": "^3.0.0"
}
}
}
}
With Custom package.json Configuration
Merge additional configuration into package.json:
{
"extra": {
"npm-installer": {
"packages": ["react", "react-dom"],
"package-json": {
"scripts": {
"build": "webpack",
"dev": "webpack serve"
},
"browserslist": {
"production": ["> 1%", "last 2 versions"],
"development": ["last 1 chrome version"]
}
}
}
}
}
How It Works
- When
composer installorcomposer updateis executed, the plugin hooks into the post-install/post-update events - If no
package.jsonexists, one is created with minimal default configuration - Packages specified in the configuration are installed using the configured package manager
- If
package-jsonconfiguration is provided, it's recursively merged into the existingpackage.json - All changes are output to the console for visibility
Troubleshooting
"npm-installer: creating package.json"
This message appears when no package.json exists. The plugin creates a basic one automatically.
"npm-installer: ... failed"
This error occurs when the package manager command fails. Ensure:
- The package manager (npm/yarn) is properly installed
- Package names and versions are correct
- You have internet connectivity to download packages
- The working directory has write permissions
No packages installed
Verify your composer.json configuration:
- The
extra.npm-installersection exists packagesordev-packagesarrays/objects are not empty- Package names are valid
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit pull requests or open issues.
Author
Lee Jones (prolificjones82)