After I use composer to install a new extension, I was trying to run bin/magento setup:upgrade
but it throws this error "Area code is not set"
I am running Magento 2.4.3-p1
I did google but did not find any result. It just happen suddenly.
If you install any custom module and when you run upgrade command and got this error then you need to add following code in your custom module setup/InstallData file...
public function __construct(
...
\Magento\Framework\App\State $state
...
) {
try {
$state->getAreaCode();
} catch (\Magento\Framework\Exception\LocalizedException $e) {
if ($e->getMessage() == 'Area code is not set') {
/* missing - after $state */
$state->setAreaCode(\Magento\Framework\App\Area::AREA_ADMINHTML); } } }
I encountered the similar issue during Magento upgrade(commerce cloud 2.3.7 to 2.4.5-p1)
. I had several custom modules installed. I found the issue when I started debugging. The issue was throwing from some of the custom commands file.
I disabled them from the respective di.xml and the errors were gone. There was something wrong in the file. I fixed the issue and ran the setup upgrade, it started executing without any errors.
Note: Don't update anything in the core files which is not recommended.
I've encountered this problem after an upgrade, after a long search i found that one of the modules in app/code had template files that didn't have a frontend or adminhtml parent folder!
That was the reason of the error. Area code was indeed not set.