Search code examples
javascriptnode.jsauthorizationtokenexpress-jwt

Why error message "TypeError: expressJwt is not a function" is appearing? How can it be fixed?


jwt.js:

const expressJwt = require('express-jwt')

function authJwt() {
    const secret = process.env.secret
    return expressJwt({
        secret,
        algorithms: ['HS256']
    })
}

module.exports = authJwt

terminal:

C:\code-wldn\final-project\wldn-api\helpers\jwt.js:5
return expressJwt({
       ^

TypeError: expressJwt is not a function
    at authJwt (C:\code-wldn\final-project\wldn-api\helpers\jwt.js:5:12)
    at Object.<anonymous> (C:\code-wldn\final-project\wldn-api\app.js:18:9)

I tried to resolve the error message

TypeError: expressJwt is not a function

by ensuring that express-jwt is properly imported and used as a function in the jwt.js file. I expected the error to be resolved, allowing the application to run without any issues related to expressJwt. However, the error persisted despite my attempts to address it.

enter image description here


Solution

  • Since you are importing the whole module, you have to specify the method to use. For example:

    const expressJwt = require('express-jwt');
    
    function authJwt() {
        const secret = process.env.secret;
    
        return expressJwt.expressjwt({
            secret,
            algorithms: ['HS256'],
        })
      }
    

    resources:By @ChocoNao