Registry / web-framework / nswag
library14.7.0jsnpmunverified

NSwag is a comprehensive toolchain designed for generating OpenAPI (Swagger) specifications from ASP.NET Web API controllers and subsequently generating client code in various languages, primarily TypeScript and C#. It supports a wide range of client frameworks including Angular, React, and jQuery, and handles both OpenAPI 2.0 (Swagger) and 3.0 specifications. The current stable version, 14.7.0, reflects active development with frequent minor releases. NSwag differentiates itself through its deep integration with the .NET ecosystem for robust spec generation and its versatile client generation capabilities. It operates predominantly as a command-line interface (CLI) tool, wrapping .NET executables, and crucially requires a compatible .NET runtime (Full .NET Framework 4.6.2+ or .NET 6.0+) to be installed on the system where the CLI commands are executed.

npm install nswag
INSTALL
IMPORT
SIG · NSWAG
N
nswag
web-frameworkjavascriptv14.7.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

nswag (global CLI command)
nswag <command> [options]
import { nswag } from 'nswag'
The primary interface is the `nswag` command-line tool, typically invoked globally after `npm install -g nswag` or via `npx`.
nswag (local CLI command)
"node_modules/.bin/nswag" <command> [options]
require('nswag').runCLI()
When installed locally (`npm install nswag --save-dev`), the executable is found in the project's `node_modules/.bin` directory.
TypeScript client generation
nswag openapi2tsclient /input:swagger.json /output:client.ts
import { generateTypeScriptClient } from 'nswag'
The `nswag` CLI provides specific commands like `openapi2tsclient` to generate TypeScript API clients from OpenAPI specifications, which is a core use case.

Demonstrates how to generate a TypeScript API client from an OpenAPI specification using the NSwag command-line tool, including runtime specification and cleanup.

import { execSync } from 'child_process'; import * as fs from 'fs'; import * as path from 'path'; // This quickstart demonstrates how to use the NSwag command-line tool // to generate a TypeScript client from an existing OpenAPI (Swagger) specification. // Replace 'your-api-swagger.json' with your actual spec file or URL. // This assumes nswag is installed globally (`npm install -g nswag`) or available via npx. const swaggerSpecPath = path.join(__dirname, 'temp-api-swagger.json'); const outputClientPath = path.join(__dirname, 'generated-api-client.ts'); // Specify the .NET runtime NSwag should use. Common options: Net80, Net90, Net100, WinX64. // Ensure a compatible .NET runtime is installed on your system. const runtime = process.env.NSWAG_RUNTIME ?? 'Net80'; // Create a dummy OpenAPI spec for demonstration if it doesn't exist if (!fs.existsSync(swaggerSpecPath)) { console.log(`Creating a dummy OpenAPI spec at ${swaggerSpecPath}`); const dummySpec = { openapi: '3.0.0', info: { title: 'Dummy API', version: '1.0' }, paths: { '/items': { get: { summary: 'Get all items', responses: { '200': { description: 'A list of items', content: { 'application/json': { schema: { type: 'array', items: { type: 'string' } } } } } } } } }, components: {} }; fs.writeFileSync(swaggerSpecPath, JSON.stringify(dummySpec, null, 2)); } try { console.log(`Generating TypeScript client from ${swaggerSpecPath} using runtime ${runtime}...`); // The 'nswag' command is resolved from PATH (global install) or node_modules/.bin (npx) const command = `npx nswag openapi2tsclient /input:${swaggerSpecPath} /output:${outputClientPath} /runtime:${runtime}`; execSync(command, { stdio: 'inherit' }); console.log(`TypeScript client generated successfully to ${outputClientPath}`); console.log('\n--- Generated Client Excerpt ---'); const clientContent = fs.readFileSync(outputClientPath, 'utf8'); console.log(clientContent.substring(0, 500) + '...\n'); // Show first 500 chars } catch (error: any) { console.error('Error generating client:', error.message); console.error('Please ensure NSwag is installed globally (`npm install -g nswag`) or available via npx, and a compatible .NET runtime is installed (e.g., .NET 8 SDK).'); process.exit(1); } finally { // Clean up dummy spec and generated client if (fs.existsSync(swaggerSpecPath)) fs.unlinkSync(swaggerSpecPath); if (fs.existsSync(outputClientPath)) fs.unlinkSync(outputClientPath); console.log('Cleaned up temporary files.'); }
nswag --version
Debug
Known issues
breakingNSwag is a wrapper around .NET executables and requires a compatible .NET runtime (Full .NET Framework 4.6.2+ or .NET 6.0+) to be installed on the system where the `nswag` CLI is executed. Operations will fail without it.
fix
Install a supported .NET SDK (e.g., .NET 8.0 or 9.0) on your system. For specific scenarios, explicitly specify the desired runtime using the `/runtime:` argument, e.g., `/runtime:Net80`.
affects: >=1.0.0
breakingStarting with NSwag v14.6.0, the minimum TypeScript output version for generated clients is 4.3. Code generated for older TypeScript versions might encounter compilation errors.
fix
Ensure your TypeScript project is configured to target at least TypeScript 4.3. If you must support older TypeScript versions, you may need to manually adjust the generated client code or stick to an older NSwag version.
affects: >=14.6.0
gotchaThe `nswag` CLI attempts to automatically detect the .NET runtime. However, mismatches between the detected runtime and the configured runtime in `nswag.json`, or if the global tool is run with a different .NET version than intended, can lead to `InvalidOperationException` or `NConsole.UnusedArgumentException`.
fix
Explicitly specify the runtime in your command, e.g., `nswag run /runtime:Net80`. If using the global .NET tool, ensure it's installed with the correct framework, e.g., `dotnet tool install --global NSwag.ConsoleCore --version <version> --framework net8.0`.
affects: >=14.0.0
securityNSwag v14.6.3 included important package updates to address CVE-2025-55315, a vulnerability in ASP.NET Core related to HTTP request smuggling. Earlier versions may be susceptible to this security flaw.
fix
Upgrade NSwag to version 14.6.3 or higher to incorporate the necessary security fixes and ensure generated clients or specifications are not derived from vulnerable components.
affects: <14.6.3
gotchaNSwag v14.6.1 introduced a regression that affected EnumValue behavior, causing unexpected output in generated code. This issue was resolved in v14.6.2 by reverting the behavior and introducing a new `JsonLibraryVersion` configuration setting.
fix
Upgrade to NSwag v14.6.2 or a later version. If customization is needed, refer to the documentation for the `JsonLibraryVersion` configuration.
affects: 14.6.1
Errors
Common errors & fixes
NConsole.UnusedArgumentException: Unrecognised arguments are present: [/runtime:Net80]
This error occurs when the `/runtime:` argument is passed to the `nswag` command, but the underlying .NET tool executable (especially when running via the global tool shim) does not expect it or conflicts with its own runtime detection.
fix
If running NSwag as a global .NET tool, install it with the specific framework: `dotnet tool install --global NSwag.ConsoleCore --version <version> --framework net8.0`. If using the npm package, ensure the runtime argument is correctly formatted and supported for the specific NSwag version.
'nswag' is not recognized as an internal or external command, operable program or batch file.
The `nswag` command-line tool is not installed globally or is not found in the system's PATH environment variable.
fix
Install NSwag globally using `npm install -g nswag`. Alternatively, if installed locally (`npm install nswag --save-dev`), invoke it via `npx nswag` or by providing the full path: `"./node_modules/.bin/nswag"`.
fix axios template: [object Object] is not valid JSON.
An issue with the generated Axios client template in certain NSwag versions where the client attempts to `JSON.parse` an object that is already parsed or not a valid JSON string.
fix
Upgrade NSwag to version 14.7.0 or higher, which includes a fix for this specific Axios template issue.
Newtonsoft.Json.JsonSerializationException: Error setting value to 'SelectedSwaggerGeneratorRaw' on 'NSwag.Commands.NSwagDocument'. ---> System.NullReferenceException: Object reference not set to an instance of an object.
This typically occurs when upgrading .NET projects (e.g., from .NET 6 to .NET 8) with existing NSwag configurations (`nswag.json`) that contain properties or settings incompatible with the newer NSwag version or its underlying Newtonsoft.Json dependencies.
fix
Review and update your `nswag.json` configuration file to match the requirements of your NSwag and .NET versions. This may involve removing deprecated settings or adjusting schema generator properties, as some settings were moved in NSwag v14.
Upgrade
Version history
14.7.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources