Skip to content

Area code not set error in magento 2

I have created one custom command. That command is used when i need to generate some specific file. When i run setup:upgrade command then i give me error like “Area code not set“.

Below is code example.

$this->state->setAreaCode(Area::AREA_FRONTEND);
if ($input->getOption(self::GENERATE_NEW_MENU)) {
    $output->writeln('<info>Generating static navigation:<info>');
    $generated = $this->abcModel->generateNavigation(1);
    if ($generated) {
        $output->writeln('<info> generated successfully.<info>');
    } else {
        if ($generated->getMessage()) {
            $output->writeln('<error>Having issue while generating static navigation<error>');
            $output->writeln('<error>Exception: ' . $generated->getMessage() . '<error>');
        }
    }
}