Add a module in the composer with GitHub repository Magento 2

Add a module in the composer with GitHub repository Magento 2

In this article, I will show you how we can add -magento 2 module in the composer.

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. Read more

1. Add module repository into Github

First of all, you need to add your module into the GitHub repository. Create a new repository into GitHub and add all module’s directory into the root of the repository.

You can check the sample module Here: https://github.com/mageprince/magento2-FAQ

2. Create Composer file

Now create compsoer.json at the root of your repository.

Here is the example of the composer file:

Url: https://github.com/mageprince/magento2-FAQ/blob/master/composer.json

{
    "name": "mageprince/module-faq",
    "description": "Magento2 FAQ Module",
    "homepage": "https://github.com/mageprince/magento2-FAQ",
    "type": "magento2-module",
    "version": "1.0.5",
    "license": "GPL-3.0-or-later",
    "authors": [
        {
          "name": "Prince Patel",
          "email": "mail.mageprince@gmail.com"
        }
    ],
    "support": {
        "issues": "https://github.com/mageprince/magento2-FAQ/issues"
    },
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Prince\\Faq\\": ""
        }
    }
}

2. Create Release on GitHub

Create a new release by click on release link on the root of GitHub repository

Now click on Draft a new release and add tag version, Release title and description and click on publish release.

Now you can see your latest release package like

3. Add module into Packagist.org

Now follow the below steps to add a module into packagist.org

  1. Go to https://packagist.org and create a new account or use an existing account.

2. Click on submit button and add Github repository URL and click on Check button

It will fetch the package name from your repository like mageprince/module-faq

Now click on the submit button to confirm. It will take approx 5 to 8 minutes to add your module into the composer. You can see the loading symbol onto the update button on the next screen.

After a successful update, you will see your latest release which you added into the GitHub repository.

That’s It..! You successfully added your module into the composer. Now run the composer require command with package name declared into composer to install the module like:

composer require mageprince/module-faq

You can check the newly installed module at path /Magento2/vendor/mageprince/module-faq

Note: If you need a private package, register onto https://packagist.com/ and add following to composer.json

{
    "repositories": [
        {
            "type": "composer",
            "url": [repository url here]
        }
    ]
}

Hope this article will help you to understand how to add a module into the composer. If you have any question please comment below. Please share this article. Happy Coding…:)