25

In Magento2 I've built a simple custom module and want to enable it by CLI:

bin/magento module:enable Vendorname_Modulename

But it is giving me the following error:

Unknown module(s): 'Vendorname_Modulename'

The module and it's files do exist in app/code/Vendorname/Modulename/

Of course I've cleared/disabled caches, cleared generation, Memcache(d) not running...

7 Answers 7

31

registration.php missing

Apparently my Vendorname_Modulename module was missing the registration.php. I'm running the latest Magento2 version from GitHub.

Every module has to register itself in the ComponentRegistrar. A typical registration.php for a module (in the root of your module) could contain:

<?php
use Magento\Framework\Component\ComponentRegistrar;
ComponentRegistrar::register(
    ComponentRegistrar::MODULE,
    'Vendorname_Modulename',
    __DIR__
);

Also see any Magento core component in app/code/Magento/ or your vendor/magento/ dir


registration.php NOT missing

In addition to this, if you DO have a registration.php file in your module, but you are still getting this error, this means that your registration.php was not loaded and Magento2 does not know about your module.

Your module should be under app/code/ (where Magento2 will search folders in app/code/ and find your module's registration.php), but if you have built your module as a Composer package, it will be in Composer's vendor/ dir and you'll need to trick Composer into loading your module's registration.php (Magento doesn't search in vendor/ by itself).

If you'd check any Magento2 module's composer.json in vendor/magento/module-*, you'll see an "autoload" section which references the registration.php file. So Composer will autoload your module's registration.php which will "tell" Magento2 where your module is located.

This is a fragment from the Magento Checkout module's composer.json:

"autoload": {
    "files": [
        "registration.php"
    ],
    "psr-4": {
        "Magento\\Checkout\\": ""
    }
}

If you have your module in a separate repository and loaded via composer, then the above is the way to go. If you do not have it in a separate repository, then your module does not belong on vendor/ but in app/code/.

2
  • 2
    I got same error when I place the registration.php file under app/code/Vendorname/Modulename/etc folder by mistake
    – Mukesh
    Commented Mar 9, 2016 at 6:09
  • 2
    Good presentation.. well explained
    – devst3r
    Commented Oct 6, 2016 at 12:10
7

Adding to: If registration.php NOT missing

Check and confirm if the module name in the module's etc/module.xml is correct(not the module name in <sequence> tag if present)

enter image description here

1
  • 2
    Good addition! Also nice that you have illustrated it with a screenshot showing the exact field it is about +1
    – 7ochem
    Commented Jan 30, 2017 at 12:51
6
<?php
use Magento\Framework\Component\ComponentRegistrar;
$name = implode('_', array_map(
    function($part) {
        return implode(array_map('ucfirst', explode('-', $part)));
    },
    array_slice(explode(DIRECTORY_SEPARATOR, __DIR__), -2, 2)
));
ComponentRegistrar::register(ComponentRegistrar::MODULE, $name, __DIR__);

You can use it for any extension without changing anything.
It works for any extension location (app/code or vendor) and for any extension type (module, translation dictionary).

3
  • Wow, this renders the presence of this file absolutely useless as it could be the same for every module. How about that for duplicate code... @Dimitry, you've beaten Magento2's dev team :P
    – 7ochem
    Commented Nov 6, 2015 at 15:24
  • This will not work for themes.
    – Maddy
    Commented Mar 30, 2016 at 15:41
  • May I ask where this is to be used?
    – EOB
    Commented Jan 31, 2020 at 9:17
1

This can also occur if permissions are not set correctly for the module directory and files. Ensure directory and files can be read by magento and web user.

2
  • Magento should not need write permissions on your module's code
    – 7ochem
    Commented Sep 1, 2017 at 14:47
  • I had this error today and restoring the permission for the module folder to 755 made this error dissapearing, thanks Commented Oct 17, 2018 at 11:37
1

In my case, my vendor folder from the app/code was not created. I only had the module name. My module was in a zip file, so I unzipped it without the vendor folder.

Please make sure, you have both folders, otherwise you will get this error: Unknown module(s): 'Vendorname_Modulename'

1

I've solved it by run sudo composer remove <your-module> remove the module directories in vendor/<your-module> & app/code/<your-module>.

Then reinstall the module again.

Note: replace the placeholder by your module name.

1

I get the same error every time after running this command.

bin/magento module:enable --clear-static-content Dibay_Fooman_PdfCore
Unknown module(s): 'Dibay_Fooman_PdfCore'

?? and my directory structure was app/code/Dibay/Fooman/PdfCore/ where my registration.php existed.

But after errors, I found my directory structure and made three new folders; I just changed it to two names in Dibay/ dir. I added just one new folder, the name "Fooman_PdfCore", and now, the structure is like "app/code/Dibay/Fooman_PdfCore/registration.php" and the error was resolved successfully with the following information.

sudo php -f bin/magento module:enable --clear-static-content 
Dibay_Fooman_PdfCore
The following modules have been enabled:
- Dibay_Fooman_PdfCore

To make sure that the enabled modules are properly registered, run 'setup:upgrade':

Cache cleared successfully.
Generated classes cleared successfully. Please run the 
'setup:di:compile' command to generate classes.
Generated static view files cleared successfully.
                     

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.