TypeScript cannot find the type definition file for body-parser
Cannot find type definition file for 'body-parser'.Short answer
This error comes from tsconfig, not from your code. TypeScript is told to load a type package that is not there. Look at the types and typeRoots options: either remove the name from them, install the matching @types package, or point typeRoots at a folder that actually holds it.
Fix it fast
1Find out which cause you have
Print the type-related options in tsconfig and check whether the package folder exists on disk.
grep -n -E "\"types\"|\"typeRoots\"" tsconfig.json ls node_modules/@types | grep body-parser2Match what you see, then jump to its fix
What does the error look like?
Cannot find type definition file for 'body-parser'.
Cause 1 of 2
types lists body-parser but nothing installs it
1Check
When types is set, TypeScript includes only the packages listed there in the global scope. A name in that list has to exist under node_modules/@types.
grep -n "types" tsconfig.json2Fix
Install the type package, or delete the name from types if you never needed it as a global.
npm install --save-dev @types/body-parser3Verify
Run the type check and confirm the error is gone.
npx tsc --noEmitConfirmed by typescriptlang.org
Cause 2 of 2
typeRoots points away from node_modules/@types
1Check
When typeRoots is set, only packages under those folders are included, and nothing from node_modules/@types. The path is relative to tsconfig.json.
grep -n -A3 "typeRoots" tsconfig.json2Fix
Add node_modules/@types back to the list, or remove typeRoots if you do not need custom folders.
{
"compilerOptions": {
"typeRoots": ["./typings", "./node_modules/@types"]
}
}3Verify
The type check passes.
npx tsc --noEmitConfirmed by typescriptlang.org
How do I stop it coming back?
- List only globals you need in types, such as node or jest.
- Keep typeRoots out of tsconfig unless you have a custom types folder.
- Run tsc --noEmit in CI so config mistakes show up early.
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.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
Express warns 'body-parser deprecated undefined extended'
body-parser deprecated undefined extended
The warning appears because urlencoded() was called without the extended option, which body-parser 1.x has warned about since version 1.4.0.
3 causes, each with a fixWeb apps
Upgrading to Next.js 16: middleware becomes proxy, and the other things that break
Missing Required default.js for Parallel Route
Next.js 16 renames middleware.ts to proxy.ts and its exported function to proxy.
6 causes, each with a fixWeb apps
TypeScript cannot resolve import './globals.css' in Next.js
ts(2882)
TypeScript now checks imports like import './globals.css' when noUncheckedSideEffectImports is on, and it finds no type for a CSS file.
2 causes, each with a fixWeb 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
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
Clerk cannot read its publishable key in a Next.js app
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY is undefined
Clerk reads NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY.
3 causes, each with a fix