I have installed express validator with npm install express-validator --save
and required it in my app.js
. However when I ran npm start
the terminal says it's not a function.
In my app.js:
var validator = require("express-validator");
app.use(validator());
In my package.json dependencies:
"express-validator": "^6.0.1"
Response from terminal:
app.use(validator());
^
TypeError: validator is not a function
at Object.<anonymous> (/mnt/e/CODING/Templates/leaftunehoney/SC2/app.js:30:9)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/mnt/e/CODING/Templates/leaftunehoney/SC2/bin/www:7:11)` at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)
What's causing the problem?
Maintainer here.
You seem to be using the latest version. express-validator v6 has been released a few days ago, and it no longer includes what was called the "legacy API".
You should migrate to the new APIs (what previously was under express-validator/check
, for example); the getting started guide* should give you enough information to help with it.
E.g.
const { check, validationResult } = require('express-validator');
app.post('/user', [
check('username').isEmail(),
check('password').isLength({ min: 5 })
], (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(422).json({ errors: errors.array() });
}
// create/update the user however you want?
});
* There are no official migration guides as of now because the new API has been around for a few years.