NestJS TypeScript 7: Fix nest build and Set Up tsc 7

nest build fails on TypeScript 7. Run tsc 7 beside TypeScript 6 to keep the Nest CLI, Swagger plugin and ts-jest working with fast type checks.

Ask ChatGPTAsk Claude
NestJS and TypeScript 7 cover: run TypeScript 6 and 7 side by side to fix nest build and keep fast type checks
On this page
  1. Does NestJS support TypeScript 7?
  2. How do you fix "does not expose the programmatic compiler API"?
  3. How do you set up NestJS TypeScript 7 side by side?
  4. Which NestJS build option should you pick?
  5. Can you build NestJS with tsc 7 and skip the Nest CLI?
  6. How do you speed up nest build with SWC and tsc 7?
  7. Using the Swagger plugin with SWC
  8. Which tsconfig options break NestJS on TypeScript 7?
  9. Does ts-jest work with TypeScript 7?
  10. Will TypeScript 7.1 fix nest build?
  11. Key takeaways

NestJS TypeScript 7 support is partial today: tsc 7 can type-check a Nest project, but nest build and nest start stop with an error when typescript resolves to 7.x, because the Nest CLI needs a compiler API that TypeScript 7.0 does not ship. The setup that works is to install both: TypeScript 6 under the name typescript for the Nest CLI, and TypeScript 7 under an alias for fast type checks.

The commands, errors and fixes below apply to @nestjs/cli 12.0.15, typescript 7.0.2 and the @typescript/typescript6 compatibility package.

Does NestJS support TypeScript 7?#

Not through the Nest CLI. TypeScript 7.0 is the native Go port of the compiler (previewed as tsgo), and its announcement says it does not ship with an API. The Nest CLI calls that API to read your tsconfig, compile, watch and run compiler plugins, so the open nest-cli tracking issue lists nest build, nest start and nest start --watch as blocked.

Tasktypescript@7 as typescriptSide-by-side setup
npx tsc --noEmitWorks, runs TS 7Works, runs TS 7
nest build (tsc builder)FailsWorks, uses the TS 6 API
nest build -b swcFailsWorks
@nestjs/swagger CLI pluginCannot runWorks through nest build
ts-jestFailsWorks, uses the TS 6 API

The framework itself is not the problem. TypeScript 7 compiles experimentalDecorators code and still emits the design:paramtypes metadata that Nest's dependency injection reads. The gap is in the tooling around the compiler.

How do you fix "does not expose the programmatic compiler API"?#

Put TypeScript 6 back under the package name typescript, either alone or through the side-by-side aliases in the next section. This is the error nest build and nest start print:

text
Error  The installed TypeScript version (7.0.2) does not expose the programmatic compiler API that the Nest CLI requires. TypeScript 7.0 ships the "tsc" executable only; the compiler API is expected to return in 7.1. Please install TypeScript 6 (e.g. "npm i -D typescript@^6") until then.

The quickest fix is the one in the message:

npm i -D typescript@^6
pnpm add -D typescript@^6
yarn add -D typescript@^6
bun add -d typescript@^6

If your project is coming from TypeScript 5, TypeScript 6 may reject your tsconfig next, so check the tsconfig fixes below.

Switching builders does not avoid the error. The CLI checks for the API at the point where it loads typescript, so -b swc, -b webpack and -b rspack stop with the same text. CLI releases that predate the check crash with tsBinary.getParsedCommandLineOfConfigFile is not a function instead.

You may hit npm before you hit Nest. On a NestJS 12 project, npm i -D typescript@^7 fails to install:

text
npm error code ERESOLVE
npm error ERESOLVE unable to resolve dependency tree

The npm output names the conflicting ranges, such as peer typescript@"^6.0.0" from @nestjs/schematics. @nestjs/swagger 12 also limits its peer range to TypeScript 5.5 or 6. Forcing the install with --legacy-peer-deps only moves the failure to nest build.

How do you set up NestJS TypeScript 7 side by side?#

Alias the @typescript/typescript6 package to typescript, and alias TypeScript 7 to @typescript/native. Tools that import typescript get the 6.0 API, while the tsc command runs TypeScript 7. These are the aliases Microsoft documents for tools that still need the old API.

Step 1. Install both packages with one command:

bash
npm i -D "@typescript/native@npm:typescript@^7.0.2" "typescript@npm:@typescript/typescript6@^6.0.2"

Your package.json now has:

json
{
  "devDependencies": {
    "@typescript/native": "npm:typescript@^7.0.2",
    "typescript": "npm:@typescript/typescript6@^6.0.2"
  }
}

Step 2. Check which compiler each command runs. tsc should report a 7.x version and tsc6 a 6.0.x version:

bash
npx tsc -v
npx tsc6 -v

The package version of @typescript/typescript6 and the compiler version it prints can differ: package 6.0.2 can report compiler 6.0.3. If npx tsc -v shows 6.x, delete node_modules, reinstall and check again.

Step 3. Add a type-check script that uses TypeScript 7:

json
{
  "scripts": {
    "typecheck": "tsc -p tsconfig.json --noEmit",
    "build": "nest build",
    "start:dev": "nest start --watch"
  }
}

Step 4. Run it in CI before the build:

bash
npm ci
npm run typecheck
npm run build
npm test

npm run typecheck is where you get the native speed. Microsoft reports that full builds are typically between 8x and 12x faster with TypeScript 7. nest build still compiles with TypeScript 6, so it gets no faster, and the default builder type-checks a second time. The SWC section below removes that duplicate work.

The same aliases work on NestJS 11 with @nestjs/cli 11, after two preparations. First, older tooling rejects TypeScript 6 as a peer dependency, so update ts-jest to 29.4.7 or later and typescript-eslint to 8.58.0 or later if you use them. Second, the NestJS 11 starter tsconfig sets baseUrl and no rootDir, and TypeScript 6 stops nest build on it, so apply the tsconfig changes below. If linting broke too, the typescript-eslint TS 7 fix uses this exact layout, so one change covers both.

NestJS TypeScript 7 side-by-side setup: the typescript alias serves the TypeScript 6 API to nest build, ts-jest and plugins, while the @typescript/native alias provides tsc 7 for type checks

Which NestJS build option should you pick?#

Pick the side-by-side setup with the default builder unless build time is a real problem. It keeps every Nest CLI feature. Move to SWC when builds are slow, and compile with plain tsc 7 only if your app uses no CLI plugins and no paths aliases.

OptionCompiler that emits JSCLI pluginspaths aliases rewrittenBest for
Stay on TypeScript 6 onlyTS 6YesYesNo time to change tooling
Side by side, default builderTS 6YesYesMost projects
Side by side, SWC builderSWCWith typeCheck on and a metadata importYesSlow builds
Plain tsc 7 buildTS 7NoNoSmall services without plugins

Can you build NestJS with tsc 7 and skip the Nest CLI?#

Yes, if you can live without CLI features. tsc -p tsconfig.build.json from TypeScript 7 compiles a Nest app that boots normally, but it never runs compilerOptions.plugins and does not rewrite paths aliases. With the side-by-side packages installed, only the scripts change (add npm i -D concurrently for watch mode):

json
{
  "scripts": {
    "build": "tsc -p tsconfig.build.json",
    "start:dev": "npm run build && concurrently \"tsc -p tsconfig.build.json --watch --preserveWatchOutput\" \"node --watch dist/main.js\""
  }
}

Use dist/src/main.js instead if your rootDir puts the output there. Check these three differences before you commit to it:

  1. Swagger metadata disappears. With the @nestjs/swagger CLI plugin, nest build adds a static _OPENAPI_METADATA_FACTORY() method to your DTO classes. Plain tsc output has none, so the build succeeds and your OpenAPI schemas lose the properties the plugin would have described for you.

  2. Path aliases break at runtime. nest build rewrites an import such as @users/users.controller.js into a relative path. tsc leaves it as written, and Node fails on startup:

    text
    Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@users/users.controller.js' imported from .../dist/app.module.js
  3. nest-cli.json is ignored. Settings such as assets and deleteOutDir belong to the Nest CLI, so copy assets and clean dist in your own scripts. If you clean dist, also set "incremental": false under compilerOptions in tsconfig.build.json. With incremental builds on, the leftover .tsbuildinfo file makes tsc treat the project as up to date, so it exits successfully and writes nothing.

NestJS build comparison on TypeScript 7: nest build keeps Swagger plugin metadata and rewrites path aliases, plain tsc 7 drops the metadata and leaves aliases that fail with ERR_MODULE_NOT_FOUND

How do you speed up nest build with SWC and tsc 7?#

Let SWC emit the JavaScript and let tsc 7 do the type checking. Keep the side-by-side packages, because the Nest CLI still needs the TypeScript 6 API to start, then install the SWC builder:

npm i -D @swc/cli @swc/core
pnpm add -D @swc/cli @swc/core
yarn add -D @swc/cli @swc/core
bun add -d @swc/cli @swc/core

For an app without CLI plugins, merge these two keys into the compilerOptions of nest-cli.json:

json
{
  "compilerOptions": {
    "builder": "swc",
    "typeCheck": false
  }
}

Then chain the type check in front of the build:

json
{
  "scripts": {
    "typecheck": "tsc -p tsconfig.json --noEmit",
    "build": "npm run typecheck && nest build"
  }
}

SWC alone compiles files that contain type errors, so do not drop the typecheck step. In watch mode, run npx tsc --noEmit --watch in a second terminal.

Using the Swagger plugin with SWC#

Set "typeCheck": true and load the generated metadata yourself. With SWC the plugin does not rewrite your DTOs. Instead, the CLI type-checks with the TypeScript 6 API and writes src/metadata.ts, which main.ts has to pass to Swagger before creating the document:

ts
import { NestFactory } from '@nestjs/core';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { AppModule } from './app.module.js';
import metadata from './metadata.js';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await SwaggerModule.loadPluginMetadata(metadata);
  const config = new DocumentBuilder().build();
  SwaggerModule.setup('api', app, () =>
    SwaggerModule.createDocument(app, config),
  );
  await app.listen(process.env.PORT ?? 3000);
}
await bootstrap();

With the plugin, reverse the build script so the CLI regenerates the metadata before tsc 7 reads it. In the other order, a missing or outdated src/metadata.ts fails the type check and nest build never runs:

json
{
  "scripts": {
    "build": "nest build && npm run typecheck"
  }
}

Run npm run build once to create src/metadata.ts, then commit it. A standalone tsc 7 check on a fresh checkout otherwise fails with TS2307: Cannot find module './metadata.js'. After renaming or removing a DTO or controller, rebuild and commit the new file before CI runs typecheck. CommonJS projects drop the .js extensions and call bootstrap() without the top-level await.

SWC can also fail on circular imports. The Nest docs recommend a wrapper type such as Relation<Profile> for circular entity relations.

Which tsconfig options break NestJS on TypeScript 7?#

baseUrl, moduleResolution: node and settings that relied on old defaults are what break. TypeScript 6 changed the defaults for strict, rootDir and types and deprecated those two options; TypeScript 7 removes them. That means nest build on TypeScript 6 reports most of these problems too.

Error on tsc 7CauseFix
TS5102: Option 'baseUrl' has been removedbaseUrl in older Nest projectsDelete it; start paths targets with ./
TS5108: Option 'moduleResolution=node10' has been removed"moduleResolution": "node"Use nodenext with "module": "nodenext"
TS5011: The common source directory ... 'rootDir' setting must be explicitly setNew rootDir defaultSet rootDir
TS2593: Cannot find name 'describe'types now defaults to []List node plus jest or vitest/globals
TS2564: Property 'name' has no initializerstrict is now on by defaultSet strictPropertyInitialization to false for DTOs

If your code imports paths such as src/users/... that only resolved through baseUrl, replace it with "paths": { "*": ["./*"] }.

ignoreDeprecations is not a way out. On TypeScript 6, baseUrl raises TS5101 and "ignoreDeprecations": "6.0" silences it, but TypeScript 7 still fails with TS5102. TypeScript 7 also rejects "target": "es5", "esModuleInterop": false, downlevelIteration and the amd, umd and systemjs module settings.

A tsconfig.json that covers all five rows looks like this. Merge these settings into your file rather than dropping the others it has:

json
{
  "compilerOptions": {
    "module": "nodenext",
    "moduleResolution": "nodenext",
    "target": "ES2023",
    "outDir": "./dist",
    "rootDir": ".",
    "strict": true,
    "strictPropertyInitialization": false,
    "types": ["node", "jest"],
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "skipLibCheck": true
  }
}

Replace jest with vitest/globals if you use Vitest. tsconfig.build.json needs its own source root so that output stays at dist/main.js. The NestJS 11 starter has neither of these two settings, so add them:

json
{
  "extends": "./tsconfig.json",
  "compilerOptions": { "rootDir": "./src" },
  "include": ["src"],
  "exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

For changes outside Nest, see the TypeScript 7 upgrade guide.

Does ts-jest work with TypeScript 7?#

Only through the alias. ts-jest 29.4.15 declares a peer range of typescript >=4.3 <7, and when typescript is 7.0.2 a test suite that it transforms fails to run with this message:

text
The TypeScript compiler "typescript" (version 7.0.2) does not expose the JavaScript compiler API required by ts-jest. To use TypeScript 7 for project type-checking, install it as "@typescript/native" and alias "@typescript/typescript6" as "typescript" for ts-jest.

Run the install command from step 1 and ts-jest compiles through the TypeScript 6 API again. NestJS 12 projects created by nest new can use Vitest instead; the NestJS 12 migration guide compares Jest and Vitest on v12 and shows how to keep an existing Jest suite running.

Will TypeScript 7.1 fix nest build?#

Not by itself. TypeScript 7.1 brings a new API under entry points such as typescript/sync, and the Nest CLI has to be changed to use it. With typescript@7.1.0-beta installed, CLI 12.0.15 prints the same "does not expose the programmatic compiler API" error, including the line saying the API is expected in 7.1.

The work in progress is nest-cli pull request 3554, which is open and unmerged. As proposed:

  • It adds one-shot nest build support for the tsc builder on TypeScript 7.1.
  • Watch mode is left for a follow-up pull request.
  • compilerOptions.plugins and paths stop the build with an error, because the new emit API accepts no custom transformers.
  • SWC with typeCheck, webpack and Rspack still require the old API.

The 7.1 iteration plan lists the release candidate for December 1, 2026 and the stable release for a day in December that is not yet set. If your app relies on the Swagger or GraphQL plugin, plan to keep the side-by-side setup past the 7.1 release. See TypeScript 7.1 beta features for the rest of that release.

Key takeaways#

  • nest build and nest start fail when typescript is 7.x, with the SWC, webpack and Rspack builders too.
  • Alias @typescript/typescript6 as typescript and TypeScript 7 as @typescript/native; then tsc is 7 and the Nest CLI and ts-jest keep the 6.0 API.
  • Run tsc 7 as a separate type-check step, and add SWC if the build itself is slow.
  • Building with plain tsc 7 drops CLI plugin metadata and leaves paths aliases unresolved.
  • Remove baseUrl, set rootDir and types, and move off moduleResolution: node.
  • TypeScript 7.1 alone does not unblock the Nest CLI; watch the open pull request.

Questions

Comments

Add to the discussion…

No comments yet. Questions, corrections and your own results are all welcome.

Be kind and stay on topic. No name needed: leave it blank and you post under an anonymous name that stays yours. Links get nofollow; spam is removed. Your email is never shown and is only used for reply notifications you ask for. Privacy