5

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.

user9507446
  • 333
  • 1
  • 3
  • 14

9 Answers9

10

2023 UPDATE

With version greater than 7.x, use this as doc said (https://www.npmjs.com/package/express-jwt) :

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

Then you can use jwt:

jwt({
  secret: "shhhhhhared-secret",
  audience: "http://myapi/protected",
  issuer: "http://issuer",
  algorithms: ["HS256"],
});
Alaindeseine
  • 3,260
  • 1
  • 11
  • 21
5

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');
juzraai
  • 5,693
  • 8
  • 33
  • 47
  • It seems to fix this error, but I get another more elaborate one: Error: Route.get() requires a callback function but got a [object Undefined] – user9507446 Aug 30 '20 at 21:05
  • If my post answers your initial question, please accept it. The error in your comment is a different situation and `Route.get()` is not present in your example code above, so please post it as a new question. :) – juzraai Aug 30 '20 at 21:15
4

this work for me without high severity vulnerability caused by low version:

  1. npm uninstall express-jwt
  2. npm install express-jwt@6.1.0
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
1
const { expressjwt: expressjwt } = require('express-jwt');

OR

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

Try lowercase this worked for me..

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0

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

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.

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
0

Following the documentation worked for me:

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

var jwtCheck = jwt({
      secret: jwks.expressJwtSecret({
          cache: true,
          ...more stuff
})
Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0

If you don't have an algorithm in the expressJwt object, add it next to the secret, separated by , and use cypher such as RS256. It happened to me.

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Nov 08 '22 at 13:25
0
const { expressjwt: ejwt } = require("express-jwt");

This worked for me, it should be expressjwt not expressJwt as documentation state.

alea
  • 980
  • 1
  • 13
  • 18