How to Package Module in Magento 2?

How to Package Module in Magento 2? You will be guided in this topic by creating a composer file composer.json before you implement the packaging of any module. With the assistance of the composer file, it is allowed to deliver, install, and upgrade components in an application instance.

4 Steps to package module in Magento 2

Step 1: Setup a Magento Composer file (composer.json)

The composer.json file allows specifying the name, Magento 2 requirements, version, and some of other basic information about the component you are concerning. However, you need to save this file in the root directory of the module.

The composer.json uses Composer’s generic schema, with some requirements:

Element Description
name A fully-qualified component name, in the format <vendor-name>/module-<component-name>. All letters must be in lowercase. Use dashes in the <component-name> to separate words.
type For modules, this value must be set to magento2-module. Other possible types are metapackage, magento2-theme, and magento2-language.
autoload Specify necessary information to be loaded, such as [registration.php](extension-dev-guide/build/component-registration.html). For more information, see Autoloading from Composer.

In Magento Marketplace, there are a number of the acceptable component types. Each component type is corresponding to one composer type in the next column of the following table. And the value of the type field you must add to composer.json for that type of component.

Friendly name composer.json `type`
Metapackage metapackage
Module magento2-module
Theme magento2-theme
Language package magento2-language

Let’s take the below example for Metapackage

{
    "name": "magento/product-community-edition",
    "description": "A sample metapackage",
    "version": "2.0.0",
    "type": "metapackage",
    "require": {
        "php": "~5.5.0|~5.6.0|~7.0.0",
        "zendframework/zend-stdlib": "~2.4.6",
        "zendframework/zend-code": "~2.4.6",
        "zendframework/zend-server": "~2.4.6",
        "zendframework/zend-soap": "~2.4.6",
        "zendframework/zend-uri": "~2.4.6",
        "zendframework/zend-validator": "~2.4.6",
        "zendframework/zend-crypt": "~2.4.6",
        "zendframework/zend-console": "~2.4.6",
        "zendframework/zend-modulemanager": "~2.4.6",
        "zendframework/zend-mvc": "~2.4.6",
        "zendframework/zend-text": "~2.4.6",
        "zendframework/zend-i18n": "~2.4.6",
        "ext-ctype": "*",
        "ext-gd": "*",
        "ext-spl": "*",
        "ext-dom": "*",
        "ext-simplexml": "*",
        "ext-mcrypt": "*",
        "ext-hash": "*",
        "ext-curl": "*",
        "ext-iconv": "*",
        "ext-intl": "*",
        "ext-xsl": "*",
        "ext-mbstring": "*",
        "ext-openssl": "*"
        },
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ]
}

And the following example is the composer.json file for a module:

{
  "name": "magento/sample-module-newpage",
  "description": "A Magento 2 module that creates a new page",
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",
    "magento/framework": "~100.0.4"
  },
  "autoload": {
    "files": [ "registration.php" ],
    "psr-4": {
      "Magento\\SampleNewPage\\": ""
    }
  }
}

Step 2: Register the component using registration.php

Next, you must register the component through registration.php.

Step 3: Package and generate the component

It is time to work with the directory of your extension, let’s apply a zip operation as the following snippet to add a package:

zip -r vendor-name_package-name-1.0.0.zip package-path/ -x 'package-path/.git/*'

There are some notes in the zip operation:

  • Don’t leave white spaces among the words
  • Insert alphanumeric characters as dashes

Like that, your extension package will be fetched from any valid GitHub URL.

Step 4: View sample composer.json

File: [composer.json]

{
  "name": "mageplaza/magento-2-seo-extension",
  "description": "Magento 2 SEO extension",
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0",
    "mageplaza/core-m2": "dev-master"
  },
  "type": "magento2-module",
  "version": "1.1.1",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "authors": [
    {
      "name": "Mageplaza",
      "email": "hi@mageplaza.com",
      "homepage": "https://www.mageplaza.com",
      "role": "Leader"
    }
  ],
  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "Mageplaza\\Seo\\": ""
    }
  }
}

File: composer.json

{
  "name": "mageplaza/magento-2-social-login",
  "description": "Magento 2 Social Login Extension",
  "require": {
    "php": "~5.5.0|~5.6.0|~7.0.0"
  },
  "type": "magento2-module",
  "version": "1.0.0",
  "license": [
    "OSL-3.0",
    "AFL-3.0"
  ],
  "authors": [
    {
      "name": "Mageplaza",
      "email": "hi@mageplaza.com",
      "homepage": "https://www.mageplaza.com",
      "role": "Leader"
    }
  ],
  "autoload": {
    "files": [
      "registration.php"
    ],
    "psr-4": {
      "Mageplaza\\SocialLogin\\": ""
    }
  }
}

Conclusion

That’s all about how to package module in Magento 2 by creating a composer file. I hope this tutorial is helpful for you. If you get any issue while following all the steps above, feel free to let me know.

Thanks for reading!

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 package module
  • magento 2 custom module development tutorial
  • magento 2 module creator
  • magento 2 module.xml sequence
  • magento 2 hello world module
  • magento 2 install module
  • how to install magento 2 extension from zip file
  • how to create package extension in magento 2
  • how to package an extension in magento 2
  • how to package magento 2 module
  • how to create module in magento 2 admin
  • 2.3.x, 2.4.x
x