Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

708
Visualizações
ExpressJwt is not a function

I'm trying to write user authorization middleware. I use this function to validate and verify authorization later on.

I have done yarn add express-jwt.

auth.js code:

const SECRET = 'secret-message';

const jwt = require('jsonwebtoken');
const expressJwt  = require('express-jwt');
const compose = require('composable-middleware');

function sign(user) {
    return jwt.sign({
        _id: user._id,
    }, SECRET, {
        expiresIn: 60 * 60
    });
}

function sendUnauthorized(req, res) {
    console.log(req.headers.authorization);
    console.log(req.user);
    res.status(401).json ({ message: 'Unathorized' });
};

const validateJwt = expressJwt({
    secret: SECRET,
    fail: sendUnauthorized,
    getToken(req) {
        if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
            return req.headers.authorization.split(' ')[1];
        } else if (req.query && req.query.access_token) {
            return req.query.access_token;
        }
        return null;
    }
});

And the error is:

const validateJwt = expressJwt({
                    ^

TypeError: expressJwt is not a function

I'm stuck on this error and can't go any further. I really need some advice.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As the error says, expressJwt is not a function. The logical next step would then be to check what it is instead, and at the same time to recheck your assumption of it being a function (why do you think that?) and looking for sources for that, for example by checking the docs of the package.

You will find that a) it's an object with { expressjwt: <some function here> }, and b) that the docs show an example that uses destructuring (var { expressjwt: jwt } = require('express-jwt')) which you don't do.

Both of these findings should point you to the fact that your assumption of the module's default export being a function is wrong and it's instead an object with property expressjwt which is the function you are looking for, so you could fix your code by using destructuring on the import as follows:

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

(Alternatively, using expressJwt.expressjwt(...) would have worked too, of course, but I think it looks quite ugly and is unnecessarily verbose.)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda