TypeError: expressJwt is not a function

Viewed 4938

I'm trying to write middleware for user authorization in my app. I use this function to check if a route requires being sign in. The code is as follows:

const { expressJwt } = require('express-jwt'); 

exports.requireSignin = expressJwt({
secret: process.env.JWT_SECRET,
algorithms: ["HS256"],
userProperty: "auth",});

However, I get the following error:

TypeError: expressJwt is not a function at Object.<anonymous> (path to the file)\

What could be the problem? None of the other answers seem to be helpful.

5 Answers

With the curly brackets

const { expressJwt } = require('express-jwt');
      ^            ^

you are trying to do object destructuring, which looks for a field named expressJwt in the object exported by express-jwt module. But according to the error message that object doesn't have such field.

As per express-jwt's documentation, you don't need destructuring but to simply assign the exported object into a variable, so try the following form (without curly brackets):

const expressJwt = require('express-jwt');

With version greater than 7.x, use this as doc said :

const { expressjwt: jwt } = require("express-jwt");

Reference to the readme.

If I am not mistaken the right function is named jwt

//const { expressJwt } = require('express-jwt');

//use this 
var jwt = require('express-jwt');

I tried all the possible solution found on multiple programming forums, but nothing worked, so I just downgrade my express-jwt version to 5.3.1 and it start working perfectly, so it means error is in new update, just uninstall current version with npm uninstall express-jwt and install again npm I express-jwt@5.3.1, it will help.

Following the documentation worked for me:

var { expressjwt: jwt } = require("express-jwt");

var jwtCheck = jwt({
      secret: jwks.expressJwtSecret({
          cache: true,
          ...more stuff
})
Related