nodejs/node · error · TemplatesNotFound

Tried to select from an empty list of templates.

Error message

Tried to select from an empty list of templates.

What it means

Raised by Environment.select_template (jinja2 environment.py) as a TemplatesNotFound when the `names` argument is empty or falsy. select_template iterates a list of candidate template names and returns the first that loads; passing an empty list (or None) means there is nothing to try, which the method treats as an explicit error rather than silently returning.

Source

Thrown at tools/inspector_protocol/jinja2/environment.py:845

            return name
        if parent is not None:
            name = self.join_path(name, parent)
        return self._load_template(name, self.make_globals(globals))

    @internalcode
    def select_template(self, names, parent=None, globals=None):
        """Works like :meth:`get_template` but tries a number of templates
        before it fails.  If it cannot find any of the templates, it will
        raise a :exc:`TemplatesNotFound` exception.

        .. versionadded:: 2.3

        .. versionchanged:: 2.4
           If `names` contains a :class:`Template` object it is returned
           from the function unchanged.
        """
        if not names:
            raise TemplatesNotFound(message=u'Tried to select from an empty list '
                                            u'of templates.')
        globals = self.make_globals(globals)
        for name in names:
            if isinstance(name, Template):
                return name
            if parent is not None:
                name = self.join_path(name, parent)
            try:
                return self._load_template(name, globals)
            except TemplateNotFound:
                pass
        raise TemplatesNotFound(names)

    @internalcode
    def get_or_select_template(self, template_name_or_list,
                               parent=None, globals=None):
        """Does a typecheck and dispatches to :meth:`select_template`
        if an iterable of template names is given, otherwise to

View on GitHub (pinned to 1b2de5e052)

Solutions

  1. Provide at least one fallback template name as the last element of the list.
  2. Guard the call: `if names: env.select_template(names) else: <default behavior>`.
  3. Ensure the list-building step always includes a guaranteed-to-exist default.

Example fix

# before
env.select_template(candidates)  # candidates may be []
# after
if candidates:
    tpl = env.select_template(candidates)
else:
    tpl = env.get_template('default.html')
Defensive patterns

Strategy: validation

Validate before calling

def safe_select(env, names, default=None):
    if not names:
        if default is None:
            raise ValueError('no template names provided')
        return env.get_template(default)
    return env.select_template(names)

Type guard

def has_candidates(names) -> bool:
    return bool(names)

Try / catch

from jinja2 import TemplatesNotFound
try:
    tpl = env.select_template(names)
except TemplatesNotFound as e:
    if 'empty list' in str(e):
        tpl = env.get_template('default.html')
    else:
        raise

Prevention

When it happens

Trigger: Calling `env.select_template([])`; passing a list that was built dynamically and ended up empty; passing None where a list was expected.

Common situations: Building the candidate list from configuration/user input that yielded zero entries; falling back through a chain of optional templates where all were filtered out upstream; defaulting a parameter to [] and not replacing it.

Related errors


AI-assisted analysis of nodejs/node@1b2de5e052 (2026-08-13). Data as JSON: /api/errors/722623f5809a8a35. Report an issue: GitHub.