expressjs / body-parser

Node.js body parsing middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bodyParser is deprecated

akshaymemail opened this issue · comments

Screenshot 2021-03-08 080753

I'm getting a warning, bodyParser is deprecated

I'm using,
Node : V15.6.0
Express : 4.17.1
BodyParser : 1.19.0
Mongoose : 5.11.19

ANY SOLUTION ?

Hm, I'm not sure why you would get that. What is generating the message and where does it come from?

code :
Screenshot 2021-03-08 083427

Warning generated by VS Code :
1
2

Thanks @akshaymemail . It sounds like it is either an issue with VS Code or the typescripts, neither of which we have created or manage. My guess is it is from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/body-parser/index.d.ts#L19 but I'm not a Typescript expert. You may want to file an issue with that project maybe, not sure. Unfortunately there isn't anything we can do here, unless you have some specific suggestion.

Thanks @dougwilson , I removed deprecated notation from file index.d.ts line no 19 and now vs code is not giving any warning 😁

Then :
tt

Now :
t2

Thanks @akshaymemail . For making this issue. I am also facing this problem. I tried many solution but does not work.

emoved deprecated notation from file index.d.ts line no 19 and now vs c

But this is not any proper solution to fix in the project. For this we need to make changes in node_modules. and that's not a right way or proper solution.

is either an issue with VS Code or the typescrip

I think this is not a problem from the vs-code side. The problem occurs due to added deprecated decorator in the typescript file.
If the method is not deprecated then why there is a deprecated decorator in the typescript file.
Because of that decorator, The intelligence of vs-code shows the message that bodyparser is deprecated.

As per my answer, if there is not needed to that decorator, then kindly remove it from the upcoming version and the problem will solve.

Hi @SekMicra this project did not make that file, which is why the issue is closed here. You can see by looking at the repo here there is no such file as well.

@SekMicra as @dougwilson already pointed out, VS Code automatically download type definitions for JavaScript projects from this Github repo:
https://github.com/DefinitelyTyped/DefinitelyTyped. So you need to file an issue there.

More specifically as already pointed out, this line in that repo is incorrect: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/body-parser/index.d.ts#L19

Could you write an issue there?

Hi @dedo1911 no need to repeat what I already wrote here back to me :) just telling me what I already said doesn't really solve anything

@dedo1911

Hey, the issue is someone has deprecated the function in index.js file in version 1.19.0.
Look at the file, shouldn't be that hard.


/**

 * Create a middleware to parse json and urlencoded bodies.
 *
 * @param {object} [options]
 * @return {function}
 * @deprecated  //---------------------------------DEPRECATED----------------------------------
 * @public
 */

  function bodyParser (options) 

Yes, that function in index.js is, indeed, deprecated. Please do not use app.use(bodyParser()) . Instead use the different functions exported for each type as documeted in the readme.

Yes, that function in index.js is, indeed, deprecated. Please do not use app.use(bodyParser()) . Instead use the different functions exported for each type as documeted in the readme.

That is not how I am using it, I am using it as the readme file instructs which is

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

The problem is, since the function is deprecated, everything derived from it will also be deprecated.

Hi @eyoeldefare how is that? How do you propose the function be deprecated then? Also seems weird that we have had it like that for years now, but suddenly it was reported multiple times. Something changed recently, but it wasn't the deprecation mark in this module's source code... Please explain how you are seeing a deprecation message for your above usage and can you show exactly how it is derived? Does editing the index.js in your node_modules folder fix the issue?

Hi @eyoeldefare how is that? How do you propose the function be deprecated then? Also seems weird that we have had it like that for years now, but suddenly it was reported multiple times. Something changed recently, but it wasn't the deprecation mark in this module's source code... Please explain how you are seeing a deprecation message for your above usage and can you show exactly how it is derived? Does editing the index.js in your node_modules folder fix the issue?

I will try that and come back real quick. Might be vcode issue too because I updated that just yesterday.

I solved it by just using express.json() and express.urlencoded() instead since I am using Express >= 4.16.0, but the problem seem to me that either vscode is messing up or something in the below code snipet.

var deprecate = require('depd')('body-parser')

exports = module.exports = deprecate.function(bodyParser,
  'bodyParser: use individual json/urlencoded middlewares')

Note it wraps the bodyParser function in deprecate.function(). Since it does that, its noted as deprecated perhaps. Somewhere there must be the issue. Anyways thanks.

Hi @eyoeldefare so are you saying that if you edited that line, the deprecation you are seeing goes away? The original error you posted ended in in index.d.ts file. You are positive the issue is in index.js file? I wonder why the message would say a different file name if that is the case.

Hey, well yeah I did removed @Deprecation from the file and it only fixed only the error showing on the bodyParser on deprecate.function(bodyParser...) function. But it didn't fix it in my application. Which is why I said the issue could have been from feeding bodyParser into a deprecate function like deprecate.function().

Also, yes, I said index.d.ts file initially because js file is compiled to ts in vscode. So that is also a possible place this bug is happening.

Okay before I leave this subject, I just want to say that this issue is most definitely from vscode compiling the js files to ts files when it catches the files and store them in our computer and most likely this happened after the new update to vscode made to typescript 4.2.3. As to why? I don't know. Now I understand that removing the @Deprecation from index.js most likely didn't affect my application because my change to the node_module file is not take effect since the files are already catched and stored on my computer. So, removing @Deprecation could actually work. I just don't know how to force vscode to make it compile my new change to the file and figuring that out can take some time. But I am sure, this info will help you guys. Peace

Thanks for the information @eyoeldefare . I'm not familiar with that environment or Typescript, so not sure how I can help. If someone can provide some definitive "changing x will fix y" we can certainly make the change. The change can be tested out without publishing a new version of this module, testing, publishing, testing again, I'm sure of it, as that would be pretty crazy 😂 . Even some definitive documentation around it would be very helpful that explains what makes the message you are seeing.

I am back again to give you guys more info for the good of humanity.
So first of all, let me say that I fixed the problem completely and it has nothing to do with @Deprecation or body-parser.

So it turns out, VSCode compiles the javascript files used inside of body-parser into typescript for lint reasons like showing us errors, warning, and better programming experience overall. So, when it does that, the catched files are stored inside AppData in our computer for ease of accessing the catched files. So once I figured that out, I simply deleted all of the catched files inside my computer, C:\Users\eyoel\AppData\Local\Microsoft\TypeScript\4.2\node_modules and I also removed node_module from my application, and npm update, now when npm updates the new files, VSCode is forced to compile that and catch the new files. That completely fixed it for me and I didn't even have to down grade the typescript version. I am still using 4.2.3 (latest). Cheers.

that doesn't work for me. i'm using Ubuntu 20.04.2 LTS with node v14.16.0 and npm v6.14.11 and the cached files are inside of ~/.cache/typescript/4.2/node_modules.

I followed your instructions and after npm update always appears the index.d.ts with the bodyParser deprecated. I have to delete the anotation as someone commented and it seems to work fine

Actually, this body-parser repo is pure javascript. Type for this project is coming from @types/body-parser, which has been updated. Express ^v4.16.0 has built-in express.urlencoded and express.json middlewares now. Please consider using this new one to avoid these warnings.

Actually, this body-parser repo is pure javascript. Type for this project is coming from @types/body-parser, which has been updated. Express ^v4.16.0 has built-in express.urlencoded and express.json middlewares now. Please consider using this new one to avoid these warnings.

Yeah I am using that too despite solving the issue on my end. Just don't want to deal with the unknown.