express-validator: express validator is not a function

I used express-validator for some of my projects, today when i installed it and i wanted to use it like this const expressValidator = require('express-validator'); app.use(expressValidator); i got the error “expressValidator is not a function” this is with version “6.1.1” i installed version “5.3.1” i used before and it worked.

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 23
  • Comments: 28 (3 by maintainers)

Commits related to this issue

Most upvoted comments

With me it worked out that way:

Uninstall the current version: npm uninstall express-validator And install the older version: npm install express-validator@5.3.1

You can change or degrade the version by running this command in your root folder. npm install express-validatior@5.3.1–save-exact

No need to delete your node modules.

Yeah, that’s a breaking change with v6.0.0.

@studentofcoding

  1. remove node_modules folder
  2. change version in package.json
  3. run npm install

@gustavohenke - I would also like to see some kind of migration from 5.x to 6.x, as I’m in the same situation as @RMS21 where I can no longer use app.use(expressValidator)

With me it worked out that way:

Uninstall the current version: npm uninstall express-validator And install the older version: npm install express-validator@5.3.1

great!!! Thank you!

Same issue brought me here but I’ve been able to resolve it, thanks to StackOverflow

Here’s my implementation. Remove: app.use(expressValidator())

Then:

var router = express.Router();
const { check, validationResult } = require('express-validator');

router.post('/register',
  [
    check('email', 'Email is not valid').isEmail(),
    check('username', 'Username field is required').not().isEmpty(),
    check('password', 'Password field is required').not().isEmpty())
  ], 
  function(req, res, next) {

  // Check Errors
  const errors = validationResult(req);
  if (errors) {
    console.log(errors);
    res.render('register', { errors: errors.array() });
  }
  else {
    console.log('No Errors');
    res.render('dashboard', { message: 'Successful Registration.' });
  }
});

But then I still have issues and I need help here

  if (errors) {
    console.log(errors);
    res.render('register', { errors: errors.array() });
  }

…the console.log(errors); is returning undefined for values that I am checking for even though I can retrieve the values with req.body.value.

A basic screenshot below: express-validator-undefined-issues

Or: link: https://express-validator.github.io/docs/ const {check, validationResult} = require(‘express-validator’);

route.post([check('nome', 'O nome é obrigatório').not().isEmpty(),
            check('email', 'O email é obrigatório').isEmail(),
            check('password', 'O password é obrigatório').not().isEmpty(),], 

(req, res)=>{

    const errors = validationResult(req);

    if(!errors.isEmpty()){

        app.utils.error.send(errors, req, res);
        return false;
    }

    db.insert(req.body, (err, user)=>{ //entering data into database

        if(err){
            app.utils.error.send(err, req, res);
        }else{
            res.status(200).json(user);
            
        }
    });

});

/utils/error.js file: module.exports = {

send: (err, req, res, code = 400)=>{

console.log(`error: ${err}`);
res.status(code).json({
    error: err
});

}

};

Captura

I have this function error, uninstall the current version and go to v 5.3.1 and I get even more errors. what should I do ?