Maaz Zindani

Loading the index…

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

TypeScript cannot find the type definition file for body-parser

ErrorCannot 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.

  • Applies to TypeScript 5.x and 6.0, Next.js 15, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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-parser
  2. 2Match what you see, then jump to its fix

    1. 1tsconfig.json has "types": [..., "body-parser"] and ls prints nothingtypes lists body-parser but nothing installs itFix
    2. 2tsconfig.json has a typeRoots entry such as ./typingstypeRoots points away from node_modules/@typesFix

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.json

2Fix

Install the type package, or delete the name from types if you never needed it as a global.

npm install --save-dev @types/body-parser

3Verify

Run the type check and confirm the error is gone.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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.json

2Fix

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 --noEmit

Confirmed by typescriptlang.org

Did this fix it?

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:

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.