Managing dependencies for a custom project

Last updated on
19 August 2022

You can use Composer to manage dependencies for your custom modules. To do this, your Drupal site's composer.json (located in the repo root) must have a way to read your custom project's composer.json file. If your custom project is not hosted on Packagist or Drupal.org, you may use a Composer path repository to accomplish this.

Find the repositories section of your root composer.json file, and modify it to include your custom module or modules as path repositories:

"repositories": [
    {
        "type": "composer",
        "url": "https://packages.drupal.org/8"
    },
    {
        "type": "path",
        "url": "docroot/modules/custom/example"
    }
]

If your repositories section looks different, keep every entry already listed there, and add only the last example path repository. Adjust the path as necessary to point to the installation location of your custom module.

Next, require your module in your project:

composer require org/example

Note that the organization that you should use to require your custom project is defined in your module's composer.json file. It can be whatever you want; usually, this matches whatever the organization is in the project's repository on GitHub or GitLab, et. al.

Include project dependencies in root composer.json

Alternately, if you wish to avoid including your custom module's composer.json file, you can follow this process to get the packages working in your local development environment.

Simply take the dependencies from your module in development, and add them to the root composer.json file. For example, the last line here is a new addition which is available via Packagist:

"require": {
  "composer/installers": "^1.2",
  "cweagans/composer-patches": "^1.6",
  "drupal-composer/drupal-scaffold": "^2.2",
  "drupal/admin_toolbar": "1.x-dev",
  "drupal/console": "~1.0",
  "drupal/core": "~8.0",
  "drupal/devel": "1.x",
  "drupal/toolbar_themes": "^1.0@alpha",
  "drush/drush": "~8.0",
  "webflo/drupal-finder": "^0.2.1",
  "webmozart/path-util": "^2.3",

  "mf2/mf2": "dev-master"
}

After you save the composer.json file, run composer update from the same directory as the composer.json file.

Your dependencies will be added to the root /vendor directory and will be detected by the autoloader as expected.

Be sure to also run drush cache-rebuild or otherwise clear caches for the best results.

Use the Wikimedia Composer Merge Plugin

Using the wikimedia/composer-merge-plugin package is deprecated. The instructions below remain for reference, primarily for the benefit of sites that are already using this technique and have not yet moved to the recommended path repository method.

 If you still wish to use the Composer Merge Plugin, you will need to require it in your project:

composer require wikimedia/composer-merge-plugin

NOTE: I found that adding the below to composer.json and THEN requiring the composer merge plugin installed everything in one go.

Merging in additional composer.json files

Reference your additional composer.json files in the extra section of your root composer.json file.

"extra": {
  "merge-plugin": {
    "require": [
      "docroot/modules/custom/example/composer.json"
    ]
  }
}

Also, you can reference entire directory to avoid referencing for each custom module:

"extra": {
  "merge-plugin": {
    "include": [
      "docroot/modules/custom/*/composer.json"
    ],
    "recurse": true
  }
}

Now you can run composer install and your custom module composer.json will get picked up:

composer install

Help improve this page

Page status: No known problems

You can: