Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

npm run build fails on TypeScript errors although npm run dev worked

Errorerror TS6133: 'x' is declared but its value is never read.

Short answer

Vite only transpiles TypeScript and never checks types, so dev works. The build script of the Vite templates runs tsc first, and tsc reports every error. Read the first error code: unused variables come from noUnusedLocals or noUnusedParameters, and missing globals come from the types option.

  • Applies to Vite 7, TypeScript 5
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Run the type checker on its own, as your build does, and read the first error. Each TS code names a different cause below.

    grep '"build"' package.json
    npx tsc --noEmit
  2. 2Match what you see, then jump to its fix

    1. 1The same code runs in npm run dev but npm run build prints TS errorsDev server does not type check, the build doesFix
    2. 2TS6133: 'name' is declared but its value is never readUnused variables or parameters are reported as errorsFix
    3. 3Errors such as implicit any or possibly null on lines that run fineStrict mode finds real type problems in the codeFix
    4. 4Errors about import.meta.env or asset imports only in the type checkVite client types are missing from the global scopeFix

What does the error look like?

  • error TS6133: 'x' is declared but its value is never read.
  • npm run build stops at the tsc step

Cause 1 of 4

Dev server does not type check, the build does

1Check

Vite's docs say it only transpiles .ts files and does not perform type checking. Check whether your build script calls tsc.

grep -n '"build"' package.json

2Fix

This is expected. Fix each reported error in the code. Do not remove tsc from build unless you add another type check to CI.

"scripts": {
  "build": "tsc --noEmit && vite build"
}

3Verify

Run the type check until it exits cleanly.

npx tsc --noEmit && echo ok

Confirmed by vite.dev

Did this fix it?

Cause 2 of 4

Unused variables or parameters are reported as errors

1Check

noUnusedLocals reports errors on unused local variables, and noUnusedParameters does the same for function parameters. Look for both in tsconfig.

grep -n 'noUnused' tsconfig*.json

2Fix

Delete the unused variable or import. For a parameter you must keep, start its name with an underscore, which the docs say is exempt. Turn the option off only if your team agrees.

// unused parameter kept for the signature
function onSelect(_event: Event, id: string) {
  return id
}

3Verify

Run the checker again.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 4

Strict mode finds real type problems in the code

1Check

strict turns on the whole family of strict options, and the docs warn that a TypeScript upgrade can add new errors under it. Check the TypeScript version and the strict setting.

npx tsc -v
grep -n 'strict' tsconfig*.json

2Fix

Fix the types in code (add the missing type or a null check). Disabling one strict flag is an option for a single known case, but not turning off strict for the whole project.

const el = document.getElementById('app')
if (!el) throw new Error('#app not found')
el.textContent = 'ready'

3Verify

Run the checker and the build again.

npx tsc --noEmit && npm run build

Confirmed by typescriptlang.org

Did this fix it?

Cause 4 of 4

Vite client types are missing from the global scope

1Check

If compilerOptions.types is set, only the listed packages are included in the global scope, so vite/client must be among them.

grep -n -A3 '"types"' tsconfig*.json

2Fix

Add vite/client to the types list.

{
  "compilerOptions": {
    "types": ["vite/client"]
  }
}

3Verify

Run the type check.

npx tsc --noEmit

Confirmed by vite.dev

Did this fix it?

How do I stop it coming back?

  • Run tsc --noEmit in CI and in a pre-commit step so errors show before the build.
  • Keep strict on and fix errors when they appear.
  • Name intentionally unused parameters with a leading underscore.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.