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

AWS部署Express应用遇TypeError问题求助(PM2环境)

解决AWS PM2环境中jsonwebtoken的TypeError问题

错误信息

0|index  | TypeError: Right-hand side of 'instanceof' is not an object
0|index  |     at Object.module.exports [as sign] (/home/ubuntu/newapp/node_modules/jsonwebtoken/sign.js:108:58)
0|index  |     at /home/ubuntu/newapp/index.js:100:21

问题场景

本地运行Express应用正常,但部署到AWS服务器用PM2启动时出现上述错误,怀疑相关代码如下:

let payload = {
  useremail: user.email,
  _id: user._id,
};
let token = jwt.sign(payload, "secretKey");

解决步骤

  • 对齐Node.js版本:本地和AWS服务器的Node版本需一致,分别执行node -v查看版本,若不一致,升级/降级服务器Node版本至本地相同版本。
  • 重装依赖并清理缓存:在AWS服务器项目目录执行以下命令,修复可能损坏的依赖:
    rm -rf node_modules package-lock.json
    npm cache clean --force
    npm install
    
    执行完成后重启PM2:pm2 restart index
  • 锁定jsonwebtoken版本:查看本地jsonwebtoken版本(npm list jsonwebtoken),在服务器安装相同版本:
    npm install jsonwebtoken@x.x.x
    
    替换x.x.x为你本地的版本号,确保依赖版本一致。
  • 验证PM2运行上下文:执行pm2 show index检查工作目录(cwd)是否为/home/ubuntu/newapp,若路径错误,重新指定目录启动:
    pm2 start index.js --cwd /home/ubuntu/newapp
    

内容的提问来源于stack exchange,提问作者sam as

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 17:20:31