Koa + TypeScript: Property 'body' does not exist on type Request
Asked Answered
E

2

16

I wanted to use koa & koa-bodyparser with TypeScript but whenever I access ctx.request.body I get an error that body doesn't exist on type Request

import Koa from 'koa'
import Router from 'koa-router'
import bodyparser from 'koa-bodyparser'

const app = new Koa()
const router = new Router()

const data = ['lorem', 'ipsum', 'dolor', 'sit', 'amet']

app.use(bodyparser())
router.post('/', (ctx, next) => {
  const phrase = ctx.request.body; // Property 'body' does not exist on type Request
  if (typeof phrase === 'string') {
    ctx.response.body = data.filter(element => element.includes(phrase))
  }
})
Elane answered 4/2, 2020 at 11:30 Comment(0)
E
24

Run npm install --save-dev @types/koa-bodyparser in a terminal while in the directory where your package.json is

This package contains types introduced by koa-bodyparser (such as request.body)

Elane answered 4/2, 2020 at 11:30 Comment(0)
O
5

If you're here from the future, there's an additional problem that can cause this. Scenario:

  1. Past you: Install koa, koa-bodyparser, @types/koa, @types/koa-bodyparser
  2. Code: Everything works great
  3. Today you: Time for an upgrade; update [all of those packages]
  4. 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.

Oxonian answered 28/10, 2022 at 18:10 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.