ApplicationsBlog

Composer Autoload Error: Module Could Not Be Initialized

By January 22, 2016 April 15th, 2020 No Comments

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
Jeremiah Small

Jeremiah Small

Jeremiah, Soliant Consulting's Director of Engineering, has been with Soliant since 2006. In his spare time, he is a volunteer member of Altadena Mountain Rescue Team. Jeremiah got undergrad and masters degrees from NYU, TSOA, specializing in set design for theater and film. The film/theater and ride/show fabrication industries led him to a career in software consulting where his creative training and general problem-solving talents have served him well.

Leave a Reply

Need to adjust your business processes quickly? We're helping clients use technology to keep their teams productive and running smoothly in these times of uncertainty. Our team can guide yours if you need help in these areas.

Talk to a Consultant