Magento 2 Area code not set
As a Magento developer we are facing lot of issue which is common while developing and customizing a module. Today we are going to discuss about How to fix the “Area code not set” issue. This error occurs mainly from four reason CRON, CLI , Setup Scripts and use of session in wrong area and it will pop up when you are either compiling or running setup upgrade command. In both of the cases, it tries to create an object using the constructor of your classes but couldn’t find the area code and it fails to load.
WHAT IS AREA CODE IN MAGENTO 2?
Magento uses area codes to define the entry point to application to load given a specific request context. There are seven distinct ‘areas’ of the system, each with their own area code:
- global
- doc
- crontab
- frontend
- adminhtml
- webapi_rest
- webapi_soap
When setting the session for customer or Order we should define the area.
It’s Fixed related to this :
- Magento 2.2 area code not set: area code must be set before starting a session.
- upgrading data.. area code not set: area code must be set before starting a session.
- Magento 2 setup upgrade import failed: area code is not set
- import failed: area code not set: area code must be set before starting a session.
- Magento 2 area code admin does not exist
- processing configurations data from configuration file… import failed: area code is not set
- import failed: area code is not set magento2
- area code “” does not exist Magento 2
For Example:
We are creating a plugin or overriding a method of core class in custom class and we have injected Magento core session manager and customer session in custom class constructor.
For overriding the existing core class method we are using preference in di.xml and keep in global onside our custom module. when we run complile and upgrade we will get “AREA CODE NOT SET” issue.
Solution:
To resolve this issue we should keep the di.xml in frontend area as we are using customer session in our class. Likewise if you are using Magento Backend session in your custom class you should keep the di.xml in adminhtml area.
Hope this article helped you in some way. Please leave us your comment and let us know what do you think? Thanks.