{"record":{"id":"e1accfec4a2f525d","repo":"nodejs/node","slug":"template-module-attribute-is-unavailable-in-async","errorCode":null,"errorMessage":"Template module attribute is unavailable in async mode","messagePattern":"Template module attribute is unavailable in async mode","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"tools/inspector_protocol/jinja2/asyncsupport.py","lineNumber":127,"sourceCode":"        if not self._environment.is_async:\n            return original_invoke(self, arguments, autoescape)\n        return async_invoke(self, arguments, autoescape)\n    return update_wrapper(_invoke, original_invoke)\n\n\n@internalcode\nasync def get_default_module_async(self):\n    if self._module is not None:\n        return self._module\n    self._module = rv = await self.make_module_async()\n    return rv\n\n\ndef wrap_default_module(original_default_module):\n    @internalcode\n    def _get_default_module(self):\n        if self.environment.is_async:\n            raise RuntimeError('Template module attribute is unavailable '\n                               'in async mode')\n        return original_default_module(self)\n    return _get_default_module\n\n\nasync def make_module_async(self, vars=None, shared=False, locals=None):\n    context = self.new_context(vars, shared, locals)\n    body_stream = []\n    async for item in self.root_render_func(context):\n        body_stream.append(item)\n    return TemplateModule(self, context, body_stream)\n\n\ndef patch_template():\n    from jinja2 import Template\n    Template.generate = wrap_generate_func(Template.generate)\n    Template.generate_async = update_wrapper(\n        generate_async, Template.generate_async)","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/tools/inspector_protocol/jinja2/asyncsupport.py#L109-L145","documentation":"Raised by the wrapped _get_default_module (asyncsupport.py) when template.module is accessed on a Template whose environment is in async mode. In async mode the module must be built by awaiting make_module_async / get_default_module_async, so the synchronous .module property is deliberately disabled.","triggerScenarios":"Accessing `template.module` (or `{% import %}`-driven module access at the API level) while environment.is_async is True; calling code that was written for sync Jinja2 against an async-enabled Environment.","commonSituations":"Porting code that reads template.module.<block>() to an async server; a third-party helper that unconditionally touches .module being reused in an async app.","solutions":["Use `await template.make_module_async(...)` (or get_default_module_async) and read attributes off the returned TemplateModule.","If you need the sync .module path, keep a separate non-async Environment for that template.","Refactor the consuming code to call the rendered block functions through the async module API."],"exampleFix":"# before\nm = template.module            # raises in async mode\nm.my_block()\n# after\nm = await template.make_module_async()\nm.my_block()","handlingStrategy":"validation","validationCode":"async def get_module(template):\n    if template.environment.is_async:\n        return await template.make_module_async()\n    return template.module","typeGuard":"def is_async_env(template) -> bool:\n    return getattr(template.environment, 'is_async', False)","tryCatchPattern":null,"preventionTips":["Never read .module directly in code that may run in async mode — route through a helper.","Keep sync and async Environments separate if both code paths exist.","Prefer await make_module_async() uniformly in async apps."],"tags":["async","jinja2","module","template"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}