'Express Validator Error: expressValidator is not a function

I'm trying to install and use express-validator package. I've installed the package version (6.0.0) and then in my server.js file the code is:

const bodyParser = require('body-parser')
const expressValidator = require('express-validator')
const express = require('express')
const nunjucks = require('nunjucks')
const sessionInMemory = require('express-session')
const cookieParser = require('cookie-parser')

Then a few lines down I've put in the following:

const app = express()
const documentationApp = express()
app.use(expressValidator())

When the server reloads the changes (using nodemon) the app crashes and says:

TypeError: expressValidator is not a function

There are other bits of code in my server.js file but I've stripped out most of it that isn't relevant I think.

Console log for expressValidator:

{ oneOf: [Function: oneOf],
  buildSanitizeFunction: [Function: buildSanitizeFunction],
  sanitize: [Function],
  sanitizeBody: [Function],
  sanitizeCookie: [Function],
  sanitizeParam: [Function],
  sanitizeQuery: [Function],
  buildCheckFunction: [Function: buildCheckFunction],
  check: [Function],
  body: [Function],
  cookie: [Function],
  header: [Function],
  param: [Function],
  query: [Function],
  checkSchema: [Function: checkSchema],
  matchedData: [Function: matchedData],
  validationResult: { [Function] withDefaults: [Function: withDefaults] },
  Result: [Function: Result] }

Code for routes.js file:

router.get('/email-adress', function (req, res) {
  res.render('email-adress', { success: req.session.success, errors: req.session.errors })
  req.session.errors = null
})

router.post('/finished', function (req, res) {
  let email = req.body.email

  req.checkBody('email', 'Email required').isEmail()

  var errors = req.validationErrors()
  if (errors) {
    req.session.errors = errors
    req.session.success = false
    res.redirect('/email-adress')
  } else {
    req.session.success = true
    res.redirect('/finished')
  }
})


Solution 1:[1]

Express Validator has been updated therefore, you can't use it this way This is a new way to use the express validator

Weather stick with the previous version or use the current syntex of it.

npm uninstall express-validator
npm install [email protected]

Solution 2:[2]

Yeah! Even I had the same problem. You can change the version by writing the command in root folder.

Command:

npm install [email protected] --save-exact

Solution 3:[3]

const { check, validationResult } = require('express-validator');
router.post('/finished', function (req, res) {
let email = req.body.email

check('email', 'Email required').isEmail()

var errors = validationResult(req)
if (errors) {
  req.session.errors = errors
  req.session.success = false
  res.redirect('/email-adress')
  } else {
  req.session.success = true
  res.redirect('/finished')
  }
})

Do this. And remove

app.use(expressValidator()) 

line.

Solution 4:[4]

Go to package.json change "express-validator": "^6.6.0" to "express-validator": "^5.3.0", manually then run npm i

Solution 5:[5]

just update the express validator , will do the tri

npm install [email protected] --save-exact

Solution 6:[6]

This happened to me because I was following an outdated (2019) tutorial. It works if you install an older version (5.3.1 worked for me). I ran into this working along with the book "Get Programming with Node.js" by Jonathan Wexler.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Ajeet Shah
Solution 3
Solution 4 Jude Okagu
Solution 5 kinginthenorth_codez
Solution 6 Joshua Brooks