Boosting Productivity in Drupal with Composer 2

A few tips with Composer Drupal will help you save time. Here they are:

Composer 2: What is it?

Composer looks for dependencies on its own. You must register repositories using the repositories key.
Next, you can download the Drupal core and libraries (packages). Drupal and Composer work great together.
To install Drupal with Composer, you need to enter the next command:

Why Composer 2, not Composer?

composer update drupal/modulename – -with-all-dependencies

Getting Started with Composer 2

1. ALWAYS (ALWAYS!) use dry-run before major changes. However, many novice developers use dry runs for any changes.
2. Speed and optimization – Composer 2 is no longer an issue, and the optimization plugins you previously installed are no longer needed. For example, “zaporylie” or “prestissimo” plugins. Some people update these plugins to versions that are compatible with Composer 2. 

  • The first of them is “composer.json”, which contains information about installed libraries. So, you install libraries for use in a specific project, and they need to be specified in the “composer.json” file. This file should be in the topmost directory of your project, this is important.  In Composer 2, you can make any repository canonical, which is a big plus.
  • “Composer.lock” keeps track of library versions – this is very important. This file stores information about the current versions. So, “composer.json” contains a list of dependencies, and “composer.lock” contains a list of installed versions. If a new employee joins the development team, the same versions that other developers already have will be automatically downloaded to his computer. Without this, Composer might just download the newest versions but not the ones it needs.

To find libraries, enter the command:

Drupal with Composer 2 – first steps & commands

where “modulename” is the module you need.

To install the dependencies you need, use the command:

$ composer create-project drupal/recommended-project [project-name-dir]

It is actually easier to remove them using the command: 

where “packagename” is the package you need.

If you want to download a module, the command will look similar:

Sometimes you may need to update a separate module manually:

It can be Drupal 8, 9 or 10 core.

php composer.phar update monolog/monolog – to update one dependency (specified as an argument).

Composer 2 is an effective solution for increasing productivity when working with Drupal. Everyone knows the situation when simple operations take a lot of time. Using Composer in Drupal makes developers’ lives easier.
To speed up workflows, web developers often use the same code many times in many projects. This code can be written not only by other people; that’s not the point. Regarding programming, rewriting code is a concern for many developers. Composer allows you to get rid of this problem. You can save parts of PHP code and use them for other projects.

$ composer require drupal/modulename

You (and other developers) can form the necessary parts of the code into libraries and use these libraries in certain projects. Some libraries are linked to other libraries, and they have a logical connection. So, one project can use different code libraries attached. Sometimes, libraries are also called packages, but the essence remains.
For the convenience of developers, a unique website was created – Therefore, when you need a particular functional element and ask Composer to load it, it automatically searches for this library at the site, not only at this site (you can specify a list of repositories). One library is often linked to others, and you need to download all the libraries to regularly operate a site element. The Composer does this automatically. Composer 2 loads all logically interconnected libraries needed for the stable operation of the component, protocol, etc. 
However, you can always configure Composer to search for repositories in other cloud storage sites – GitHub, etc. The main thing is that the Composer will independently search for the necessary libraries, load them into the system, and check the system requirements. In addition, internal conflicts between previously installed and new libraries are also checked. Composer has some simple tools that make a developer’s life easier. You need to perform several steps to use the required library in a project, which we will discuss later in the article. 
Composer 2 is ideal for managing large amounts of code. By automating many processes, developers spend much less time. You can install Drupal core, themes, and modules using Composer.

The Composer will install the dependencies listed in the composer.json file. We have already said above – the versions must be listed in the composer.lock file.

$ composer require [vendor]/[packagename]

There are several reasons why you should opt for Composer 2, but the major one is its enhanced functionality. Composer 2 has been greatly improved over the first version. This allowed us to get rid of many problems, conflicts, and improve productivity and speed. Let’s take a closer look at the main improvements:
Offline mode
Composer 2 can work offline. This is very convenient if you need help with the Internet and need to get work done. Every developer knows what a “deadline” is.
Optimizations Drupal by Composer 2
The new version of Composer has significantly improved interaction with the processor and memory, which improves performance. Many minor bugs have also been fixed, improving performance and saving a lot of developer time.
Parallel loading
Also, the parallel download feature is available from the very beginning, and no need to install add-ons. This also improved performance.
Smart installation
The “vendor” folder is modified only after all required libraries have been installed.
There are many more large and small changes; we have only described the main ones. To talk about all the innovations and updates in detail, a separate article is needed.

composer remove zaporylie/composer-drupal-optimizations

7. “Scaffold” plugin may throw an error after updating to Composer 2. It’s simple – update the plugin to version 8.9 or later. Older versions work only with Composer 1.

8 tricks to work efficiently with Composer 2 in Drupal

8. There are some packages that have security problems. During installation, you may not always be aware of these issues. You can include a security feature in your project to prevent such packages from being installed. This can be done by using two commands:

$​ php composer.phar install

– -dry-run

To get the latest dependency updates, use the update command:

First, we need to understand – Composer works with libraries using just 2 files: composer.json and composer.lock. 

php composer.phar update – to update all dependencies.

6. Error when executing the “composer require” and “composer update” commands. If you see that error when entering these commands, you must immediately update the “cweagans/composer-patches” package. Composer 2 only works with version 1.7.0 or later.

This is a very simplified description, but it is completely correct and allows you to understand how Composer interacts with libraries and repositories. And, remember – “composer.lock” files are interoperable between 1 & 2 Composer versions.

If you want to preview the update and see – what would happen without any real changes, you need to:

Similar Posts