RicoSuter/NSwag · error · InvalidOperationException
The template directory \
Error message
The template directory \"{CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory}\" does not exist What it means
During ConvertToAbsolutePaths, NSwag converts the TypeScript client generator's TemplateDirectory to an absolute path and validates it exists. If set but not found on disk, this InvalidOperationException is thrown while serializing the document with absolute paths.
Solutions
- Correct the templateDirectory value to point at an existing directory.
- Use an absolute path, or ensure the document's base directory/working directory matches where the relative path resolves.
- Remove the templateDirectory setting if default templates are intended.
Example fix
// before (nswag.json) "templateDirectory": "./templates" // after "templateDirectory": "C:/project/templates"
Defensive patterns
Strategy: validation
Validate before calling
var td = doc.CodeGenerators?.OpenApiToTypeScriptClientCommand?.TemplateDirectory;
if (!string.IsNullOrEmpty(td) && !Directory.Exists(Path.GetFullPath(td, basePath)))
throw new DirectoryNotFoundException($"Template directory not found: {td}"); Try / catch
try { var json = document.ToJsonWithRelativePaths(); }
catch (InvalidOperationException ex) when (ex.Message.Contains("template directory") && ex.Message.Contains("does not exist"))
{ Console.Error.WriteLine($"Check templateDirectory in nswag.json: {ex.Message}"); throw; } Prevention
- Use absolute paths for templateDirectory in shared documents.
- Copy template directories along with the document between machines.
- Run the CLI from the directory the relative paths assume, or set the document path accordingly.
- Validate referenced directories exist before running generation in CI.
When it happens
Trigger: CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory is non-null and non-empty but the directory does not exist (relative path resolved against the wrong base directory, path typo, or directory deleted).
Common situations: Documents using custom TypeScript templates moved between machines or run from a different working directory, so relative template paths no longer resolve; typos in the templateDirectory setting.
Related errors
- The template directory \
- The template directory
- This UI does not support multiple documents per UI: Do not…
- The OpenAPI/Swagger document
- No registered OpenAPI/Swagger document found for the…
AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14).
Data as JSON: /api/errors/e180b5f29d8457b4.
Report an issue: GitHub.
Appendix: source
Thrown at src/NSwag.Commands/NSwagDocumentBase.cs:333
SwaggerGenerators.AspNetCoreToOpenApiCommand.Project = ConvertToAbsolutePath(
SwaggerGenerators.AspNetCoreToOpenApiCommand.Project);
SwaggerGenerators.AspNetCoreToOpenApiCommand.MSBuildProjectExtensionsPath = ConvertToAbsolutePath(
SwaggerGenerators.AspNetCoreToOpenApiCommand.MSBuildProjectExtensionsPath);
SwaggerGenerators.AspNetCoreToOpenApiCommand.WorkingDirectory = ConvertToAbsolutePath(
SwaggerGenerators.AspNetCoreToOpenApiCommand.WorkingDirectory);
}
if (CodeGenerators.OpenApiToTypeScriptClientCommand != null)
{
CodeGenerators.OpenApiToTypeScriptClientCommand.ExtensionCode = ConvertToAbsolutePath(
CodeGenerators.OpenApiToTypeScriptClientCommand.ExtensionCode);
CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory = ConvertToAbsolutePath(
CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory);
if (CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory != null && !Directory.Exists(CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory))
{
throw new InvalidOperationException($"The template directory \"{CodeGenerators.OpenApiToTypeScriptClientCommand.TemplateDirectory}\" does not exist");
}
}
if (CodeGenerators.OpenApiToCSharpClientCommand != null)
{
CodeGenerators.OpenApiToCSharpClientCommand.ContractsOutputFilePath = ConvertToAbsolutePath(
CodeGenerators.OpenApiToCSharpClientCommand.ContractsOutputFilePath);
CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory = ConvertToAbsolutePath(
CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory);
if (CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory != null && !Directory.Exists(CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory))
{
throw new InvalidOperationException($"The template directory \"{CodeGenerators.OpenApiToCSharpClientCommand.TemplateDirectory}\" does not exist");
}
}
if (CodeGenerators.OpenApiToCSharpControllerCommand != null)
{
View on GitHub (pinned to 63daf8fcc3)