If you're here from the future, there's an additional problem that can cause this. Scenario:
- Past you: Install koa, koa-bodyparser, @types/koa, @types/koa-bodyparser
- Code: Everything works great
- Today you: Time for an upgrade; update [all of those packages]
- Code:
Property 'body' does not exist on type Request
TL;DR: yarn.lock
What's happened?
Digging deeper, in your node_modules, you see:
- node_modules/
- @types/koa/
- @types/koa-bodyparser/
- node_modules/
- @types/koa/
Why does that matter?
Let's take a look at @koa-bodyparser to find out. Here are the relevant lines, and how it works:
import * as Koa from "koa";
declare module "koa" {
interface Request {
body?: any;
rawBody: string;
}
}
What it does is define extra parameters for the Request type on the koa
package that you're using, right? Well, normally, but not quite. It also has a dependency on @types/koa
because of the import
at the top. What it actually does is define extra parameters for the Request type on the koa
object that IT uses, and since it's now using a different version than you are, you don't have that benefit anymore.
OK why did this happen, then?
When you first installed everything, yarn built a dependency tree, looked at the versions in each package.json and ended up with this:
"@types/koa@*", "@types/koa@^2.13.1":
version "2.13.1"
... resolved, integrity, dependencies, etc.
The @types/koa@^2.13.1
will be whatever version you installed in your package.json. The @types/koa@*
is what's in @types/koa-bodyparser.
And then when you updated, yarn built a dependency tree, looked at the versions in each package.json and compared that to what it already had. Your code now says @types/koa@^2.13.5
, so yarn needs to install a new version for it. @types/bodyparser still says @types/koa@*
, which is already accounted for in the yarn.lock, and so you end up with a yarn.lock that looks like this:
"@types/koa@*":
version "2.13.1"
... resolved, integrity, dependencies, etc.
"@types/koa@^2.13.5":
version "2.13.5"
... resolved, integrity, dependencies, etc.
And now that you're not using the same version, one gets installed in your node_modules, and the other gets installed in the node_modules of the package that requested it.
So how do I fix it?
The easiest way? Delete the whole block of @types/koa@*
from your yarn.lock file and run yarn
again. You'll now end up with this:
"@types/koa@*", "@types/koa@^2.13.5":
version "2.13.5"
... resolved, integrity, dependencies, etc.
And everything is happy again until next time.