Why is a template being looked in a theme and not in my module? I have created a custom module. in that module I have create a custom twig template using hook_theme function

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_settings' =&gt; array(
      'variables' =&gt; array(
        'role' =&gt; NULL,
      )
    )
  );
}


Then I created a custom block using my module here is the code of the block

public function build() {
    $config = \Drupal::config('mymodule.config');
    $role = $config-&gt;get('role');
    return array(
      '#theme' =&gt; 'mymodule_settings',
      '#role' =&gt; $role,
    );
  }


I created a templates directory in my module and inside that directory I have created mymodule-settings.html.twig file.

Cleared the cache but Drupal gives me the following error.

Twig_Error_Loader: Template "themes/subtheme/templates/mymodule-settings.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "themes/subtheme/templates/mymodule-settings.html.twig" in the Drupal theme registry.). in Twig_Loader_Chain-&gt;getCacheKey() (line 45 of themes/basetheme/templates/block.html.twig).


My theme is a subtheme. Why Drupal is looking for the template in the theme and not in my module?
Try like this:

function mymodule_theme($existing, $type, $theme, $path) {
  return array(
    'mymodule_settings' =&gt; array(
      'variables' =&gt; array(
        'role' =&gt; NULL,
      )
      'template' =&gt; 'mymodule-settings',
    )
  );
}


You are missing to define template.