Managing dependencies with Composer Manager

Prerequisites:

  • Drupal 8
  • Composer

Use Case:

  • You want to add dependencies to your custom module using composer update at the root of your site directory.
  • Updating Drupal core removes any custom dependencies added to the root composer.json, potentially breaking any modules that require them.

Quick run-down:

First, download composer manager to your site’s modules directory and run the init.php command to register the ‘drupal-update’ command. This command is what will look through your module’s composer.json files and update the main composer.json file at your site’s docroot.

cd mysiteroot
drush dl composer_manager
php modules/composer_manager/scripts/init.php
composer drupal-update
In your sites directory, add a composer.json file and make sure you have both the name and requirements:
{
  "name": "drupal/mymodule",
  "require": {
    "mailchimp/mailchimp": "2.0.6"
  }
}

Now, run composer drupal-update again and you should see your dependencies in the vendor directory.

See also:
https://bojanz.wordpress.com/2015/09/18/d8-composer-definitive-intro/
https://www.drupal.org/node/2405811