Magento 2 Module – When to Place a Module in app/code Folder

composermagento2module

When would you place a module in the app/code folder versus another
location?

If I add new module in app/code folder with composer.json and another module in vendor folder using composer

What will be the difference ?

  • I am aware about that if module is in vendor folder then It can be
    updated by composer.
  • Does that same thing apply with the module which is in app/code
    folder
    ?
  • When you update module which is in app/code folder using composer
    they install in vendor directory.

So, What is the point of Composer.json in app/code folder Module ?

Best Answer

As you already pointed out, composer is a package management tool. Any external modules/packages which constitutes your Magento application will be managed by the composer and will be available under vendor directory, provided you have adopted the composer way of installation/ updating of your Magento instance. Magento core modules are also treated as external packages which constitute your Magento application.

app/code expects those Magento modules which are "unique" to your Magento application. Those jobs or customization which we cannot achieve by a third party/external modules has to be here. In most of the time, these modules are not installed or updated using composer. In most of the projects, your version system generally holds and manage your app/code folder only (There are other folder files we need to keep inside a versioning system, but none of them as relevant as modules inside app/code.

But it is a good practice to keep a composer.json file for those modules too. Reasons are many:

  • Magento recommends it as a good practice.
  • You can specify your module dependencies there. ex: php version, core module dependencies etc.
  • If in case, if you want to make your module a general third-party extension, then it would be so easy.

If you need more details of composer.json files which are using in Magento, you can follow this official documentation about the composer.json file.

Related Topic