Why does the call to node.js body-parser fail despite the fact that I have installed it?

req.body undefined
check if req.body is empty
body-parser example
body-parser form-data
express body-parser strict
express post request body
urlencoded in node js
body-parser-xml

I'm beginning to learn node.js and trying to work out how to get the contents of a POST request. I am trying to follow the instructions in this post. So far I have successfully installed node.js (on Windows 7) and express, and been able to get my very first script to work. However my problem comes when I try to use body-parser. I have installed it and it appears to be there

Here is the code of the node.js script

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(express.json());       // to support JSON-encoded bodies

app.get('/', function(req, res) {
    res.setHeader('Content-Type', 'text/plain');
    res.end('Vous êtes à l\'accueil');
});

app.get('/user/:usernum', function(req, res) {
    res.setHeader('Content-Type', 'text/plain');
    res.end('You are on page USER with n° : ' + req.params.usernum);
});

// https://stackoverflow.com/questions/5710358/how-to-get-post-a-query-in-express-js-node-js
app.post('/adonis', function(req, res) {
    res.setHeader('Content-Type', 'text/plain');
    console.log(req.body.title);
//    res.write(JSON.stringify(req));
    res.end('Hopefully I stringified a POST');
});

// ... Tout le code de gestion des routes (app.get) se trouve au-dessus

app.use(function(req, res, next){
    res.setHeader('Content-Type', 'text/plain');
    res.status(404).send('Page introuvable !');
});

app.listen(8091);

Yet when I run it, node.js throws an error saying "cannot find module body-parser". What have I done wrong?

As per @Kale's and others' suggestions I tried installing body-parser locally, but this does not seem to help since now my script gives the following message:

Error: Most middleware (like json) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
at Function.Object.defineProperty.get (d:\smartguide\nodejs\node_modules\express\lib\express.js:99:13)
at Object.<anonymous> (d:\smartguide\nodejs\oc1.js:5:16)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:501:10)
at startup (node.js:129:16)
at node.js:814:3`

I tried installing "json" locally and globally - the install seems to work but it makes no difference to the file error.

As Kevin B stated, you have to install body-parser locally and save it to the manifest:

npm install --save body-parser

Express Request Body Undefined Error while parsing JSON, . var express = require("express"); var app = express(); app. Method- what you want the server to do, like do you want the server to POST, PUT, GET or DELETE Header- Contains the meta-data of your request Body- Contains the information which is required to process the request.

This answer is much simpler. Go to the base directory and link to the required global modules.

npm link body-parser

There's no need to install modules all over the place. If the module is not installed globally, the above command will install the module globally then link to it locally.

bodyParser is deprecated express 4, module parses the JSON, buffer, string and URL encoded data submitted using HTTP POST request. Node.js: Request.body undefined (with body-parser) Why does the call to node.js body-parser fail despite the fact that I have installed it? Express and Body-Parser not returning values; Why use body-parser module in node.js? Parsing AngularJS http.post data on server side with express & body-parser; body-parser does not work, req.body is undefined

I think I was doing something fundamentally wrong - I went back to basics and started all over again, this time making sure I had a package.json file. Now it works.

Here is the code:

var express = require('express');
var session = require('cookie-session');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var jsonParser = bodyParser.json();

var app = express();


// JSON testing
app.post('/json-test', jsonParser, function(req, res) {
    if (!req.body) return res.sendStatus(400);
    console.log(JSON.stringify(req.body));
    console.log(req.body.title);
    res.status(200).send(req.body.title);
    })

// Can't get anything else
.use(function(req, res, next){
    res.setHeader('Content-Type', 'text/plain');
    res.status(404).send('Page introuvable !');
    })


.listen(8090);

And here is the package.json

{
"name": "todo1",
"version": "0.1.0",
"dependencies": {
    "express": "~4.11.0",
    "ejs": "~2.1.4",
    "cookie-session": "~1.1.0",
    "body-parser": "~1.10.1"
},
"author": "Martin",
"description": "Un gestionnaire de todolist ultra basique"
}

What does body-parser do with express?, How do I read a request body in node JS? Why does the call to node.js body-parser fail despite the fact that I have installed it? Tags ajax android angular api button c++ class database date dynamic exception file function html http image input java javascript jquery json laravel list mysql object oop ph php phplaravel phpmysql phpphp post python sed select spring sql string text time

I was getting the same error where after installing express I am getting errors like

Cannot find module 'body-parser' after installing this the error is

Cannot find module 'merge-descriptors' and so on for

Cannot find module 'finalhandler'

Cannot find module 'array-flatten'

These all module are dependencies for express. If you execute "npm install" or "npm install -g" without any module it will install all the missing dependencies.

To fix this I first uninstall the express and then install the same and immediately after that executed "npm install". This fixed all the errors.

Web Development with Node and Express: Leveraging the JavaScript Stack, In Nginix, there is a separate server configuration block for HTTP and HTTPS. If you fail to set the X-Forwarded-Protocol in the configuration block fool‐ing your application into thinking that the connection is secure even though it isn't. we link in body-parser, cookie-parser, express-session app.use(require('csurf')());  And what does it do actually and how? I don't understand why we need body-parser in an Express application, as we can get data without it. 49749/can-we-work-without-body-parser-in-node-js

Express body-parser middleware, parse.failed' , and the body property is set to the entity value that failed parsing. entity verify failed. This error will occur  body-parser. Node.js body parsing middleware. Parse incoming request bodies in a middleware before your handlers, available under the req.body property.. Note As req.body’s shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting.

[PDF] Web Development with Node and Express, body-parser is now its own package, which no longer includes the multipart The installation is so simple, as a matter of fact, that it can be summed up in The function is simple: it calls fs. package (despite the name, this package works fine with Express 4.0). If you fail to do this, npm will be unable to construct the. Why to Use Node.js? Here are some reasons which explain why you need to use Node.js technology: Simple. Chat is one of the most typical multiple user app. The application of chat is one of the best examples of Node.js. It has high traffic and contains data intensity and is lightweight as well. The application works on different devices at a time.

Why Use Node.js? A Comprehensive Tutorial with Examples, Node.js was never created to solve the compute scaling problem. You definitely don't want to use Node.js for CPU-intensive operations; in fact, using it for heavy using NPM, a tool that comes by default with every Node.js installation. code, fails to work, the after prompting for cnt attempts to call for mfntapes cnt times. body-parser. Node.js body parsing middleware. Parse incoming request bodies in a middleware before your handlers, available under the req.body property.. Note As req.body's shape is based on user-controlled input, all properties and values in this object are untrusted and should be validated before trusting.

Comments
  • how did you install body-parser? was it npm install --save body-parser?
  • Likely because you installed it globally instead of locally.
  • Having installed body-parser locally (as per @Kale), I now get the following error:
  • This is a common error when deploying node with heroku, and it happens when you do not add body-parser within package.json dependencies at your app root folder "dependencies": { "body-parser": "^1.17.2", ...
  • In case this helps anyone, I dont have body-parser anywhere in my package.json so I was confused when the error reported cannot find module body-parser. I solved the issue by essentially reinstalling - deleting the node_modules folder and running the npm installcommand.