First of all, I have to tell you that I have spent a lot of time researching and I have done all the solutions I have came across but I'm still getting this error when I run setup:di:compile

The full error is as follows:

Compilation was started.
Interception cache generation... 7/7 [============================] 100% 3 mins 384.0 MiB
Generated code and dependency injection configuration successfully.

  Area code not set: Area code must be set before starting a session.  

  Area code is not set

I'm trying to test some functionality and made a quick command line to test it. My entire class is as follows:

namespace Falak\MyCommands\Console;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class CustomerCredit extends Command

    protected $transaction;
    protected $appState;

    public function __construct
        \Magestore\Customercredit\Model\Transaction $transaction,
        \Magento\Framework\App\State $appState,
        array $data = []
        $this->appState = $appState;
        $this->transaction = $transaction;

    protected function configure()
            ->setDescription('Test Customer Credit');


    protected function execute(InputInterface $input, OutputInterface $output)
        $originalArea = $this->appState->getAreaCode();
        $output->writeln("Hello World!");



  1. I have tried setting the area code in the constructor directly
  2. I have tried setting it in the configure
  3. I have tried to set it every where at the same time
  4. When I remove all reference to \Magestore\Customercredit\Model\Transaction it compiles without any problems. But I need this reference as I'm trying to call some of the functions in this class

UPDATE: Here is my di.xml file:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
            <argument name="commands" xsi:type="array">
                <item name="commandTest" xsi:type="object">Falak\MyCommands\Console\CommandTest</item>
                <item name="testCustomerCredit" xsi:type="object">Falak\MyCommands\Console\CustomerCredit</item>
  • Did you define di.xml file in etc folder?
    – akgola
    Sep 24, 2018 at 7:26

1 Answer 1


Please define di.xml file in Module Falak\MyCommands\etc folder and put below code

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Console\CommandList">
            <argument name="commands" xsi:type="array">
                <item name="falak:credit" xsi:type="object">Falak\MyCommands\Console\CustomerCredit</item>

Now clean all cache and run setup:di:compile and let me know if you still have issue.

You are also missing code to get area code in protected function execute():

try {
        $areaCode = $this->appState->getAreaCode();
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        // if area code is not set then magento generate exception "LocalizedException"
  • I have updated my question with the di.xml file I'm already using. I have also tried moving it completely and using your di.xml with the same result (without the last </type> as I think it's a typo) Sep 24, 2018 at 7:45
  • Now check my updated answer and let me know.If you are still facing issue just move di.xml file in etc/frontend folder and run command again.
    – akgola
    Sep 24, 2018 at 7:52
  • Unfortunately, I have tried with the same result. Which puzzles me as I expect this to work since now I'm catching the LocalizedException. But it's still being thrown when I compile. Sep 24, 2018 at 8:06
  • 1
    Ok,I think it seems another issue.Please follow this extension as this have same functinality github.com/olegkoval/magento2-regenerate_url_rewrites
    – akgola
    Sep 24, 2018 at 8:09
  • Hello @HassanAl-Jeshi ,is your issue resolved?
    – akgola
    Sep 24, 2018 at 13:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.