Using Composer to install npm and bower packages on production (i.e. no devDependencies) Using Composer to install npm and bower packages on production (i.e. no devDependencies) php php

Using Composer to install npm and bower packages on production (i.e. no devDependencies)


You could always make use of PHP to do environment detection for you, then install other dependencies from the same script. This isn't nice and clean, like including npm and bower in post-install-cmd, but it will get you what you're looking for.

"post-install-cmd": [     "php artisan clear-compiled",     "php artisan optimize",     "php path/to/installer.php" ]

Example installer.php:

// Logic to determine the environment. This could be determined many ways, and depends on how your// application's environment is determined. If you're making use of Laravel's environment// capabilities, you could do the following:$env = trim(exec('php artisan env'));// Clean up response to get the value we actually want$env = substr($env, strrpos($env, ' ') + 1);$envFlag = ($env === 'production')    ? '--production'    : '';// Install npmpassthru("npm install {$envFlag}");// Install bowerpassthru("bower install {$envFlag}");

You could make this example more robust, and even create an Artisan command for it.


This would work;

"post-update-cmd": [        "php artisan clear-compiled",        "php artisan optimize",        "npm install",        "bower install"    ],"post-install-cmd": [    "php artisan clear-compiled",    "php artisan optimize",    "npm install --production",    "bower install --production"]

i.e. you should be running 'update' on your dev environment, and only ever run 'install' on your production environment.


It's a bit of hack but you can get the parent command's PID with $PPID in bash. From that you can get the commandline arguments.

"post-install-cmd": [    "ps -ocommand= -p $PPID | grep no-dev > /dev/null && echo called with no-dev || echo called without no-dev",],

If you were to do this, I'd put it in a bash script and run it like this: run-if-env-is-production.sh "bower install --production"

I would recommend @kwoodfriend's solution over this though, as this is less portable as it requires bash, ps & grep.