question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ts-node: Property 'body' does not exist on type 'Request'.

See original GitHub issue

Problem/Feature Request Summary

There seem to be something wrong with the typings of koa-body. You sometimes get unexpected results when used together with ts-node:

/Users/m41n/source/repos/koabody/node_modules/ts-node/src/index.ts:261
    return new TSError(diagnosticText, diagnosticCodes)
           ^
TSError: ⨯ Unable to compile TypeScript:
src/controller.ts(5,39): error TS2339: Property 'body' does not exist on type 'Request'.

    at createTSError (/Users/m41n/source/repos/koabody/node_modules/ts-node/src/index.ts:261:12)
    at getOutput (/Users/m41n/source/repos/koabody/node_modules/ts-node/src/index.ts:367:40)
    at Object.compile (/Users/m41n/source/repos/koabody/node_modules/ts-node/src/index.ts:557:11)
    at Module.m._compile (/Users/m41n/source/repos/koabody/node_modules/ts-node/src/index.ts:439:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/m41n/source/repos/koabody/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)
[nodemon] app crashed - waiting for file changes before starting...

Environment Information

  • koa-body: 4.0.4
  • koa: 2.5.2
  • node: 10.6.0

Steps to Reproduce

reproducible repo: https://github.com/jwldnr/koabody

  • install dependencies using yarn
    -> cd koabody && yarn

  • start nodemon
    -> yarn serve

… initial compile is ok (?). (no errors)

  • open src/controller.ts and edit the console.log line while nodemon is running

e.g. console.log("initial", email)
-> console.log("changed", email)

Error:
TSError: ⨯ Unable to compile TypeScript: src/controller.ts(5,39): error TS2339: Property 'body' does not exist on type 'Request'.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

9reactions
UncleYeecommented, Nov 13, 2018
{
  "compilerOptions": {
    "types": [
      "koa-body",
    ]
  },
}

5reactions
dovecclcommented, Aug 23, 2018

I think its the defect of ts-node, similar problem occurs to me. For the same project:

  • run tsc will compile without error
  • run ts-node will get: error TS2459: Type ‘Request’ has no property ‘body’ and no string index signature

I tried to add additional config to tsconfig.json, and then the error disappeared:

{
	"compilerOptions": {
		"typeRoots": [
			"./node_modules/@types",
			"./node_modules/koa-body"
		],
		// ...
	}
}

It is because tsc could find node_modules/koa-body/index.d.ts, but ts-node can’t. ts-node will only search d.ts files under node_modules/@types.

@MarkHerhold I think you can move index.d.ts to @types/koa-body

Read more comments on GitHub >

github_iconTop Results From Across the Web

Property 'body' does not exist on type 'Request' - Stack Overflow
body -parser had been removed from express 4 into separate project, so there won't be any type definition about it. I use it...
Read more >
ts-node: Property 'body' does not exist on type 'Request'. #109
Explicitly import koaBody in the file where 'request.body' is used can solve this problem. import koaBody from 'koa-body'; ...
Read more >
Unable to compile TypeScript. Property 'user' does not exist on ...
Hello, I getting annoyed about typescript in my node api. I have written a jwtverify.ts. At first typescript has not accepted the req.user....
Read more >
How to Improve Your Express Application | by Hoang Dinh
We can see, req.user will throw a TypeScript error — Property 'user' does not exist on type 'Request… . Then we have to...
Read more >
How to fix 'Property does not exist on type Window in ...
Creating an index.d.ts file somewhere in our project. · Editing the file to define the types on the Window object. · (optional) adding...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found