{"record":{"id":"424578253cf43355","repo":"nodejs/node","slug":"the-environment-was-not-created-with-async-mode-en","errorCode":null,"errorMessage":"The environment was not created with async mode enabled.","messagePattern":"The environment was not created with async mode enabled\\.","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"tools/inspector_protocol/jinja2/asyncsupport.py","lineNumber":60,"sourceCode":"\ndef wrap_generate_func(original_generate):\n    def _convert_generator(self, loop, args, kwargs):\n        async_gen = self.generate_async(*args, **kwargs)\n        try:\n            while 1:\n                yield loop.run_until_complete(async_gen.__anext__())\n        except StopAsyncIteration:\n            pass\n    def generate(self, *args, **kwargs):\n        if not self.environment.is_async:\n            return original_generate(self, *args, **kwargs)\n        return _convert_generator(self, asyncio.get_event_loop(), args, kwargs)\n    return update_wrapper(generate, original_generate)\n\n\nasync def render_async(self, *args, **kwargs):\n    if not self.environment.is_async:\n        raise RuntimeError('The environment was not created with async mode '\n                           'enabled.')\n\n    vars = dict(*args, **kwargs)\n    ctx = self.new_context(vars)\n\n    try:\n        return await concat_async(self.root_render_func(ctx))\n    except Exception:\n        exc_info = sys.exc_info()\n    return self.environment.handle_exception(exc_info, True)\n\n\ndef wrap_render_func(original_render):\n    def render(self, *args, **kwargs):\n        if not self.environment.is_async:\n            return original_render(self, *args, **kwargs)\n        loop = asyncio.get_event_loop()\n        return loop.run_until_complete(self.render_async(*args, **kwargs))","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/inspector_protocol/jinja2/asyncsupport.py#L42-L78","documentation":"Raised by Template.render_async (in jinja2 asyncsupport.py) when the template's environment was not constructed with async support enabled. render_async is the coroutine variant of render(); it requires that Environment(enable_async=True) was set at construction time so that the async code path is wired in.","triggerScenarios":"Calling `await template.render_async(...)` on a Template produced by an Environment whose enable_async kwarg was False or omitted; reusing a sync-configured Environment (e.g. a shared Flask/Django env) for async rendering; loading asyncsupport's render_async onto a sync environment.","commonSituations":"Migrating a sync Jinja2 setup to an async web framework (Starlette, FastAPI, aiohttp) and forgetting to flip enable_async; sharing one module-level Environment across sync and async code paths; upgrading Jinja2 and assuming async is on by default.","solutions":["Construct the Environment with enable_async=True: `Environment(loader=..., enable_async=True)`.","If you must keep a sync Environment, call the regular render()/generate() instead of the *_async variants.","For async frameworks, follow their Jinja2 integration docs (e.g. starlette's Jinja2Templates) which set enable_async for you."],"exampleFix":"# before\nenv = Environment(loader=FileSystemLoader('tpl'))\nawait env.get_template('x.html').render_async()\n# after\nenv = Environment(loader=FileSystemLoader('tpl'), enable_async=True)\nawait env.get_template('x.html').render_async()","handlingStrategy":"validation","validationCode":"def async_env(**kw):\n    from jinja2 import Environment\n    kw.setdefault('enable_async', True)\n    return Environment(**kw)\n\nassert env.is_async, 'enable_async was not set on this Environment'","typeGuard":"def supports_async(template) -> bool:\n    return getattr(template.environment, 'is_async', False)","tryCatchPattern":null,"preventionTips":["Centralize Environment construction in one factory so enable_async is never forgotten.","For async frameworks, use their official Jinja2 integration (it sets enable_async).","Do not share a sync Environment with async-rendering code."],"tags":["async","jinja2","config","rendering"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}