Saturday, March 28, 2020

ReferenceError: bodyParser is not defined



Error Blog 

Hi Guys ,
This blog is about the error  ReferenceError: bodyParser is not defined

Problem Statement

unable to start node js server .


Error Details 

error Log

ERROR DETAILS 

ReferenceError: bodyParser is not defined
    at Object.<anonymous> (F:\abc\study\gitRepo\new\learn\nodejs\expressjs\server.js:13:9)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)



Error Code 

You may have body parser in your code .


var express = require('express');
var bodyParser = require('body-parser');
var app = express();


var users = require('./service/users.js');

app.use(express.static('public'));

//app.get('/', function (req, res) {
  // res.send('Hello World');
//})

app.use('/users'users);
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var server = app.listen(8082function () {
   var host = server.address().address;
   var port = server.address().port;

   console.log("Example app listening at http://%s:%s"hostport)
})
  

Solution

body parser should be installed 

Code Changes 

execute the following command 

  
        

> npm install body-parser

         

OUTPUT
npm WARN saveError ENOENT: no such file or directory, open 'F:\abc\study\gitRepo\new\learn\nodejs\expressjs\package.json'  
npm WARN enoent ENOENT: no such file or directory, open 'F:\abc\study\gitRepo\new\learn\nodejs\expressjs\package.json'     
npm WARN expressjs No description
npm WARN expressjs No repository field.
npm WARN expressjs No README data
npm WARN expressjs No license field.

+ body-parser@1.19.0
updated 1 package and audited 316 packages in 3.986s
found 0 vulnerabilities



Thanks for reading . Let me know your thoughts in the comments below    .

No comments:

Post a Comment

ec2-user@ec2 Permission denied