为什么出现错误消息“TypeError:expressJwt is not a function”?怎么解决?

问题描述 投票:0回答:2

jwt.js:

const expressJwt = require('express-jwt')

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

module.exports = authJwt

终端:

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)

我尝试解决错误消息

类型错误:expressJwt 不是函数

确保express-jwt被正确导入并用作jwt.js文件中的函数。我预计该错误能够得到解决,从而允许应用程序运行而不会出现任何与expressJwt相关的问题。然而,尽管我尝试解决该错误,但该错误仍然存在。

javascript node.js authorization token express-jwt
2个回答
0
投票

根据express-jwt文档所说,应该使用以下语法导入:

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

0
投票

由于您要导入整个模块,因此必须指定要使用的方法。例如:

const expressJwt = require('express-jwt');

function authJwt() {
    const secret = process.env.secret;

    return expressJwt.expressjwt({
        secret,
        algorithms: ['HS256'],
    })
  }

资源:@ChocoNao

© www.soinside.com 2019 - 2024. All rights reserved.