How to use Composer to install Lazy-load module and its dependency

Last updated on
8 July 2021

The Lazy-load module and its dependency lazysizes library can easily be installed programmatically via composer.

Composer installation process

In order to add these dependencies via composer, you need to update your composer.json file a little bit.

Add a new repository source

First, add asset-packagist.org as a new repository source, which contains the extra packages.

"repositories": [
    {
        "type": "composer",
        "url": "https://asset-packagist.org"
    },
]

Add the requirements with composer

Require the Lazy-load module, the lazysizes javascript library and the oomphinc/composer-installers-extender plugin:

composer require drupal/lazy:^3.0 bower-asset/lazysizes:^5.1 oomphinc/composer-installers-extender:^2.0 --no-update

Your composer.json should now have the following requirements added:

"require": {
    "drupal/lazy": "^3.0",
    "bower-asset/lazysizes": "^5.1",
    "oomphinc/composer-installers-extender": "^2.0"
}

Define a new installer-type for where to install the new requirements

Add the new installer-types to extra section, to use Asset Packagist downloads as composer packages.

Update the libraries group in installer-paths for the new installer types:

"extra": {
    "installer-types": ["bower-asset", "npm-asset"],
    "installer-paths": {
        ...
        "web/libraries/{$name}": ["type:drupal-library", "type:bower-asset", "type:npm-asset"],
        ...
    }
}

Run composer

Run composer update to download the new requirements and install the library to the specified location.

The end-result

Here's how it should look, if we put them all together (This is not a complete Composer.json file, unrelated parts omitted):

"repositories": [
    {
        "type": "composer",
        "url": "https://packages.drupal.org/8"
    },
    {
        "type": "composer",
        "url": "https://asset-packagist.org"
    }
],
"require": {
    "composer/installers": "^1.0",
    "drupal/lazy": "^3.0",
    "oomphinc/composer-installers-extender": "^1.0",
    "bower-asset/lazysizes": "^5.1"
},
"extra": {
    "installer-types": ["bower-asset", "npm-asset"],
    "installer-paths": {
        "web/core": ["type:drupal-core"],
        "web/modules/contrib/{$name}": ["type:drupal-module"],
        "web/modules/{$name}": ["type:unity-module"],
        "web/profiles/contrib/{$name}": ["type:drupal-profile"],
        "web/themes/contrib/{$name}": ["type:drupal-theme"],
        "web/themes/{$name}": ["type:unity-theme"],
        "web/libraries/{$name}": ["type:drupal-library", "type:bower-asset", "type:npm-asset"],
        "drush/contrib/{$name}": ["type:drupal-drush"]
    }
}

Also, make sure to check out Installing Drupal 8 Modules documentation for more details, and clear step-by-step instructions.

Help improve this page

Page status: No known problems

You can: