TypeError: expressJwt不是一个函数

huangapple go评论78阅读模式
英文:

TypeError: expressJwt is not a function

问题

我正在尝试为我的应用程序编写用户授权中间件。我使用这个函数来检查路由是否需要登录。
代码如下:

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

exports.requireSignin = expressJwt({
  secret: process.env.JWT_SECRET,
  algorithms: ["HS256"],
  userProperty: "auth",
});

然而,我遇到了以下错误:

TypeError: expressJwt 不是一个函数,在 Object.<anonymous> (文件路径)
可能是什么问题?其他答案似乎都没有帮助。
英文:

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(&#39;express-jwt&#39;); 

exports.requireSignin = expressJwt({
secret: process.env.JWT_SECRET,
algorithms: [&quot;HS256&quot;],
userProperty: &quot;auth&quot;,});

However, I get the following error:

TypeError: expressJwt is not a function at Object.&lt;anonymous&gt; (path to the file)\

What could be the problem? None of the other answers seem to be helpful.

答案1

得分: 10

2023更新

如果版本大于7.x,请按照文档所示使用(https://www.npmjs.com/package/express-jwt):

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

然后你可以使用 jwt

jwt({
  secret: "shhhhhhared-secret",
  audience: "http://myapi/protected",
  issuer: "http://issuer",
  algorithms: ["HS256"],
});
英文:

2023 UPDATE

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

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

Then you can use jwt:

jwt({
  secret: &quot;shhhhhhared-secret&quot;,
  audience: &quot;http://myapi/protected&quot;,
  issuer: &quot;http://issuer&quot;,
  algorithms: [&quot;HS256&quot;],
});

答案2

得分: 5

使用花括号

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

你正在尝试进行对象解构,它会在由express-jwt模块导出的对象中查找名为expressJwt的字段。但根据错误消息,该对象没有这样的字段。

根据express-jwt的文档,你不需要解构,只需将导出的对象赋值给一个变量,因此请尝试以下形式(不使用花括号):

const expressJwt = require('express-jwt');
英文:

With the curly brackets

const { expressJwt } = require(&#39;express-jwt&#39;);
      ^            ^

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(&#39;express-jwt&#39;);

答案3

得分: 4

这对我来说没有由低版本引起的高严重性漏洞问题:

  1. npm uninstall express-jwt
  2. npm install express-jwt@6.1.0
英文:

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

答案4

得分: 1

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

**OR**

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

尝试将首字母小写,这对我有效。
英文:
const { expressjwt: expressjwt } = require(&#39;express-jwt&#39;);

OR

const { expressjwt } = require(&#39;express-jwt&#39;);

Try lowercase this worked for me..

答案5

得分: 0

参考 readme

如果我没记错的话,正确的函数名是 jwt

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

//使用这个
var jwt = require('express-jwt');
英文:

Reference to the readme.

If I am not mistaken the right function is named jwt

//const { expressJwt } = require(&#39;express-jwt&#39;);

//use this 
var jwt = require(&#39;express-jwt&#39;);

答案6

得分: 0

我在多个编程论坛上尝试了所有可能的解决方案,但都没有奏效,所以我只是将 express-jwt 版本降级为 5.3.1,然后它就开始完美地工作了,这意味着错误出在新的更新中,只需使用 npm uninstall express-jwt 卸载当前版本,然后再次安装 npm I express-jwt@5.3.1,它会有所帮助。

英文:

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.

答案7

得分: 0

按照文档操作对我有效:

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

var jwtCheck = jwt({
      secret: jwks.expressJwtSecret({
          cache: true,
          ...更多内容
})
英文:

Following the documentation worked for me:

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

var jwtCheck = jwt({
      secret: jwks.expressJwtSecret({
          cache: true,
          ...more stuff
})

答案8

得分: 0

如果您在expressJwt对象中没有算法,就在密钥旁边添加它,用逗号分隔,使用像RS256这样的加密算法。这种情况也发生在我身上。

英文:

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.

答案9

得分: 0

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

This worked for me, it should be `expressjwt` not `expressJwt` as documentation state.
英文:
const { expressjwt: ejwt } = require(&quot;express-jwt&quot;);

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

huangapple
  • 本文由 发表于 2020年8月31日 04:38:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63661915.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定