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

98 views Asked by At

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

2

There are 2 answers

4
Iván Muñiz On BEST ANSWER

As per express-jwt documentation says, it should be imported using the following syntax:

var { expressjwt } = require("express-jwt");
0
boubaidja bachir On

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