I have some of my own modules written on Drupal 7 that use composer packages. These composer packages are downloaded with the module Composer Manager.

How can I set explicitly the dependency on a composer package on my own module?

Should I describe it somehow on my module's .info-file or should I do it via a hook?

It's because if I don't do it, the Composer Manager can remove these packages from the site on the next updating. Unfortunately, I couldn't find any example.

  • Surely you put them in your module’s composer.json? – Clive Mar 19 '20 at 15:56
  • No, I didn't. Could you share an example for a custom module? – Yakimkin Roman Mar 19 '20 at 16:02
  • That wasn't meant to sound rude by the way, I meant "Surely one would put them in composer.json" :) I don't have an example, and it's been a long time since I user D7, but I thought the point of Composer Manager was that it recurses through module folders looking for composer.json files, and merging them into one to be installed. You can probably take a contrib module's file as an example, I guess the main point would be to make sure the require section is sensible and has your dependencies in it – Clive Mar 19 '20 at 16:14

You can add dependencies in your module.

Navigate to your module's root folder. Run composer init. When you get to the question Would you like to define your dependencies (require) interactively, say yes. You can then declare any composer libraries your module depends on.

You can also manually create a composer.json file that looks something like this:

    "name": "uniquenamespace/packagename",
    "description": "This module does some stuff",
    "type": "drupal-module",
    "authors": [
            "name": "Some Guy",
            "homepage": "https://www.example.com/some_guy",
            "role": "Maintainer"
    "require": {
        "somevendor/somepackage": "^1.9"
    "license": "GPL-2.0-or-later"

Run composer validate to check that your composer.json file is valid.

  • Thank you. A question. How should I name the module? I mean the "name" property. And is there another way? For example, a hook to define the dependency. – Yakimkin Roman Mar 19 '20 at 17:05
  • I use jaypan/packagename. The first part needs to be unique in the internet, so use something no one else will use. packagename should be your Drupal module machine name. – Jaypan Mar 19 '20 at 17:06
  • Yes, it works. Thank you. – Yakimkin Roman Mar 20 '20 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.