Magento – How to install modules in app/code with composer in Magento 2

composermagento2module

How can I force composer to install modules in app/code instead of vendor directory in Magento 2?

I have few modules they strictly need to be install in app/code, but when I update them with composer, they install in vendor directory.

Best Answer

I'm answering my own question, so it will be beneficial for someone else later,

To be able to install in a custom path, your module must support composer/installers so add it to your module's require section, eg:

"require": {
   "magento/magento-composer-installer": "*",
   "composer/installers": "~1.0"
}

and then give your module a custom path in extra section, where you want it to be installed. Eg:

"extra": {
   "installer-paths": {
      "app/code/Vendor/Module": ["vendor/module"]
   }
 }

and your module type must be "type": "magento-library" not "type": "magento2-module" (don't worry changing the type will not break anything, it'll still work like before)

so the complete json for the module will be like something this

{
    "name": "vendor/module",
    "description": "some description about your module",
    "require": {
        "magento/magento-composer-installer": "*",
        "composer/installers": "~1.0"
    },
    "type": "magento-library",
    "version": "1.0.0",
    "license": [
        "OSL-3.0",
        "AFL-3.0"
    ],
    "extra": {
        "installer-paths": {
            "app/code/Vendor/Module": ["vendor/module"]
        }
    }
}

and just to be sure, add it to your main composer.json's extra section too, this maybe optional but I have added it anyway.

"extra": {
   "magento-force": "override",
     "installer-paths": {
       "app/code/Vendor/Module": ["vendor/module"]
   }
 }

Then run composer update, so it will install your module in app/code or your given path.

Related Topic