You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NodeJS报错TypeError: JwtStrategy不是构造函数,MEAN栈认证求助

解决TypeError: JwtStrategy is not a constructor问题

嘿,这个错误的根源一眼就能看出来——你在导入JwtStrategy的时候拼写出错啦!

你现在写的是:

const JwtStrategy = require('passport-jwt').Stategy;

这里的Stategy少了一个字母r,正确的拼写应该是Strategy。因为passport-jwt包导出的构造函数是JwtStrategy,对应内部的Strategy类,拼写错误会导致你导入的是undefined,自然就会抛出“不是构造函数”的错误。

修正后的完整代码示例

// passport.js
const JwtStrategy = require('passport-jwt').Strategy; // 这里修正了拼写错误
const ExtractJwt = require('passport-jwt').ExtractJwt;
const User = require('../models/user');
const config = require('../config/database');

module.exports = function (passport) {
  // 后续的JWT策略配置逻辑...
}

额外排查建议

如果修正拼写后仍然遇到问题,可以再确认这两个点:

  • 已经通过npm install passport-jwt或yarn add passport-jwt正确安装了依赖包
  • User模型和config/database的导入路径没有错误,确保文件存在且路径正确

内容的提问来源于stack exchange,提问作者Dan Diaconu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:37:01