npm run build fails on TypeScript errors although npm run dev worked
error 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.
Fix it fast
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 --noEmit2Match what you see, then jump to its fix
- 1
The same code runs in npm run dev but npm run build prints TS errorsDev server does not type check, the build doesFix - 2
TS6133: 'name' is declared but its value is never readUnused variables or parameters are reported as errorsFix - 3
Errors such as implicit any or possibly null on lines that run fineStrict mode finds real type problems in the codeFix - 4
Errors about import.meta.env or asset imports only in the type checkVite client types are missing from the global scopeFix
- 1
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.json2Fix
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 okConfirmed by vite.dev
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*.json2Fix
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 --noEmitConfirmed by typescriptlang.org
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*.json2Fix
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 buildConfirmed by typescriptlang.org
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*.json2Fix
Add vite/client to the types list.
{
"compilerOptions": {
"types": ["vite/client"]
}
}3Verify
Run the type check.
npx tsc --noEmitConfirmed by vite.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
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 fixWeb apps
TypeScript refuses JSX because the jsx option is not set
TS17004: Cannot use JSX unless the '--jsx' flag is provided.
TypeScript compiles JSX only when you name the file with a .tsx extension and enable the jsx compiler option.
2 causes, each with a fixWeb apps
TypeScript rejects a component that returns an array or other non-Element
Its return type 'Element[]' is not a valid JSX element.
TypeScript before 5.1 required every component to return JSX.Element or null, so arrays, strings and Promises were rejected even though React renders them.
2 causes, each with a fixWeb apps
Cypress refuses to start with 'Your configFile is invalid'
Your configFile threw an error from: /path/to/cypress.config.ts
Cypress could not load cypress.config.js or .ts, or found something wrong in it.
5 causes, each with a fixWeb apps
ESLint cannot decide which copy of the @typescript-eslint plugin to use
The message lists two file paths for the same plugin, each with the name of the config that loaded it
ESLint found the same plugin at two different file paths and will not pick one.
3 causes, each with a fixWeb apps
ESLint says it cannot resolve a path to a module that does exist
Unable to resolve path to module 'react'. import/no-unresolved
import/no-unresolved fails when eslint-plugin-import cannot find the file or package on disk with its resolver.
4 causes, each with a fix