javanode.jsexpressjwtexpress-jwt

TypeError: expressJwt is not a function


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.


Solution

  • 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');