Cannot find module 'body-parser'

Asked

Viewed 463 times

-5

Node code shows this error: Alguém sabe o que pode ser?

  • 3

    Welcome to [en.so]! You have posted an image of the code and/or error message. Although it sounds like a good idea, it’s not! One of the reasons is that if someone wants to answer the question, they can’t copy the code and change something inside. See more about this at this link - How NOT to ask questions manual

  • 1

    A tip, never post pictures of codes, put in the code, and only put pictures of screens that would be a front-end, or mobile application. Place database relationship images but never code. Enter the code.

  • Important you [Dit] your question and explain objectively and punctually the difficulty found, accompanied by a [mcve] of the problem and attempt to solve. To better enjoy the site, understand and avoid closures and negativities worth reading the Stack Overflow Survival Guide in English.

2 answers

3

You put the wrong lib name.

Is not const bodyparser = require('body-parse').

Forgot an "r".

The right thing is const bodyparser = require('body-parser')

2

Using the express version >= 4.16.0 is no longer necessary to install the body-parser to use the express. Follow the pr link with the merge: https://github.com/expressjs/express/issues/2211 That was merged on 25/09/2017.

The correct way is to use express itself to enable body parser features, follow an example:

const express = require('express');

const app = express();

app.use(express.json()); //esta linha ativa o body-parser.

app.use(require('./server/index'));

module.exports = app;

If you do not want to use this approach, you should install the lib body-parser.

To do this just use the command:

$ npm install body-parser

Here is a full example of how to use this strategy:

const express = require('express');
const bodyParser = require('body-parser');
const cors = require('cors');

const app = express();
cors({ credentials: true, origin: true });
app.use(cors());
app.use(bodyParser.json({ type: 'application/json' }));
app.use(bodyParser.urlencoded({
    extended: true
}));

app.use('/', require('./server/index'));
//declaracao de rotas
app.get('*', function(req, res) {
    res.status(404).json({msg: "not found"})
});

module.exports = app;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.