3

I maintain a module on Drupal.org, the jQuery Colorpicker module, which has a requirement on a 3rd party library. I created a composer.json file in the module root. When I originally installed it, I navigated to the module root and ran composer update, which created vendor folder in my module directory, and loaded the library to this vendor directory. I then released a beta version of the module.

One user who downloaded the library ran composer drupal-update from core, which installed the library to the DRUPAL_ROOT/vendor folder. I'm thinking this is the correct way to go.

So I deleted the vendor folder from my module folder, as well as the composer.lock folder from my module folder.

However, when I run composer drupal-update in my drupal root, it does not install the library to my vendor folder.

I'm not really sure where to go with this - do I need to add something to my module, or is there a required command that needs to be run somewhere for Drupal to install the library when running composer drupal-update? I haven't worked a lot with composer, so I'm trying to find the proper way to do this so my users can use the module in the standard Drupal way.

  • You should install your colorpicker module via composer as well. (require it in Drupal root) It will download all required dependencies as well. – Neograph734 Jan 15 '17 at 10:02
  • I just ran composer require drupal/jquery_colorpicker in root, and I got the error "[InvalidArgumentException] Could not find package drupal/jquery_colorpicker at any version for your minimum-stability (dev)." – Jaypan Jan 15 '17 at 10:07
  • 1
    Did you create the file according to specifications? – Neograph734 Jan 15 '17 at 11:39
  • No I didn't, and thanks for the link, but I decided to drop usage of composer for the module, as it prevents users who aren't good with the command line from being able to manually install the module. So I just released a new version that doesn't use composer. Thanks though! I voted your comment up. – Jaypan Jan 15 '17 at 12:36
2

In order to have composer download a 3rd party library, you need (as you have already done) a composer.json file within your module. This file should contain the required 3rd party libraries. There are however quite strict guidelines for creating a proper composer.json file for Drupal in which you should at least define the following:

{
    "name": "drupal/example",
    "description": "This is an example composer.json for example module.",
    "type": "drupal-module",
    "license": "GPL-2.0+"
}

Running composer from the module root is indeed not recommended as this might become quite time consuming when installing many modules, not to mention updating all of them once in a while.

Luckily the Drupal guys have done some work, allowing composer to manage all Drupal dependencies, which is out of beta since a few weeks.

This allows you to use composer from the Drupal root to require modules as well. During the downloading of the module all dependenies of it will be resolved as well.

On a small sidenote; Reading through that blog post again leads me to assume the following should also be included in your module's composer.json file (as of writing the first link also shows an outdated message):

    "repositories": { 
        "drupal": {
            "type": "composer",
            "url": "https://packages.drupal.org/8" 
        }
    }

As for your situation, it seems you should commit a file similar to this to Drupal.org:

{
    "name": "drupal/jquery_colorpicker",
    "description": "jquery_colorpicker module.",
    "type": "drupal-module",
    "license": "GPL-2.0+",
    "repositories": { 
        "drupal": {
            "type": "composer",
            "url": "https://packages.drupal.org/8" 
        }
    },
    "require": {
        ...
    }
}

And then run composer require drupal/jquery_colorpicker from your Drupal root.

UPDATE: After reading that blog post for the fourth time, I came to conclude that the repositories part is with high probability only required for the root composer file and thus not required for individual modules.

  • Finally a computer to write a real answer :) – Neograph734 Jan 15 '17 at 18:52
  • Very nice. That's exactly what I was looking for. – Jaypan Jan 15 '17 at 23:36

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.