Home / Blog / Composer Autoload Error: Module Could Not Be Initialized

Composer Autoload Error: Module Could Not Be Initialized

So you’re working on a ZendFramework (ZF2+) project with a fellow developer, and her latest PR which adds a new module is accepted into develop, so of course you rebase your work onto it.

Instantly your dev environment blows up!

Fatal error:
Uncaught exception 'ZendModuleManagerExceptionRuntimeException' with message 'Module (FooBar) could not be initialized.' in /vagrant/vendor/zendframework/zend-modulemanager/src/ModuleManager.php:195

Stack trace:
#0 /vagrant/vendor/zendframework/zend-modulemanager/src/ModuleManager.php(169): ZendModuleManagerModuleManager->loadModuleByName(Object(ZendModuleManagerModuleEvent))
#1 /vagrant/vendor/zendframework/zend-modulemanager/src/ModuleManager.php(96): ZendModuleManagerModuleManager->loadModule('Foo\Ba...')
#2 [internal function]: ZendModuleManagerModuleManager->onLoadModules(Object(ZendModuleManagerModuleEvent))
#3 /vagrant/vendor/zendframework/zend-eventmanager/src/EventManager.php(490): call_user_func(Array, Object(ZendModuleManagerModuleEvent))
#4 /vagrant/vendor/zendframework/zend-eventmanager/src/EventManager.php(214): ZendEventManagerEventManager->triggerListeners('loadModules', Object(ZendModuleManagerModuleEvent), Array) in /vagrant/vendor/zendframework/zend-modulemanager/src/ModuleManager.php on line 195

If you’re using Composer for autoloading, you probably just forgot to run composer install to add the new module namespace into the generated autoload config.

For example, suppose her new module was added to composer.json like this:

"autoload": {
    "psr-4": {
        "Foo\Application\": "module/Application/src/",
        "Foo\Bar\": "module/Bar/src/"
    }
},

Until you run composer install, your vendor/composer/autoload_psr4.php will be missing her new namespace; hence the new error.

Solution

composer install

Leave a Comment

Your email address will not be published. Required fields are marked *