Magento 2 Module – Can’t Register Custom Module

magento2module

I created a module.xml under the app/code/my/module/etc folder.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="my_module" setup_version="1.0.0" />
</config>

Than I tried to enable the module with following command

php -f bin/magento module:enable --clear-static-content my_module

And get this error:

Unknown module(s): 'my_module'

Why don't Magento find the module?


Here is the error after executing the flush command:

[InvalidArgumentException]
There are no commands defined in the "cache" namespace.


Now here is the error:

Additionally to the xml syntax in the module.xml I added comments. These comments caused the error.

Best Answer

Well you have to first clear the cache by running the following command.

php -f bin/magento cache:clean

or

php -f bin/magento cache:flush

Then as you executed cache commands, it is possible that permissions are revoked on many files which are being executed during module enabling and disabling.

So if you're on linux(Ubuntu/Debian) then provide full permissions to your root magento directory as below:

cd /path/to/magento2
sudo chmod -R 777 .

Then, provided you've already created 'registration.php' file in your module directory(magento2root/app/code/My/Module/) like below,

<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'my_module',
    __DIR__
);

run the module enable command you mentioned in your question.

It is not mandatory to have 'composer.json' for module enable-disable, composer.json is needed for git-based tasks(commit module code/update module code etc.) if you have active repo of your site at Github.