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

Heroku部署遇SyntaxError: Unexpected token ...问题求解决方案

Heroku部署报错:SyntaxError: Unexpected token ...

问题描述

在Heroku上部署代码时触发语法错误,提示SyntaxError: Unexpected token ...,报错位置在connect-mongo依赖的MongoStore.js文件中。使用Mongo Atlas作为数据库,代码仓库地址为github.com/laxitnahar/temp。

完整报错日志

2022-08-05T11:57:48.831721+00:00 heroku[web.1]: 状态从崩溃变为启动中
2022-08-05T11:57:51.114009+00:00 heroku[web.1]: 启动进程,命令为 `npm start`
2022-08-05T11:57:52.585567+00:00 app[web.1]:
2022-08-05T11:57:52.585595+00:00 app[web.1]: > backend@1.0.0 start /app
2022-08-05T11:57:52.585596+00:00 app[web.1]: > node server.js
2022-08-05T11:57:52.585596+00:00 app[web.1]:
2022-08-05T11:57:52.809406+00:00 app[web.1]: /app/node_modules/connect-mongo/build/main/lib/MongoStore.js:75
2022-08-05T11:57:52.809420+00:00 app[web.1]: constructor({ collectionName = 'sessions', ttl = 1209600, mongoOptions = {}, autoRemove = 'native', autoRemoveInterval = 10, touchAfter = 0, stringify = true, crypto, ...required }) {
2022-08-05T11:57:52.809423+00:00 app[web.1]: ^^^
2022-08-05T11:57:52.809423+00:00 app[web.1]: SyntaxError: Unexpected token ...
2022-08-05T11:57:52.809423+00:00 app[web.1]: at createScript (vm.js:56:10)
2022-08-05T11:57:52.809423+00:00 app[web.1]: at Object.runInThisContext (vm.js:97:10)
2022-08-05T11:57:52.809424+00:00 app[web.1]: at Module._compile (module.js:542:28)
2022-08-05T11:57:52.809424+00:00 app[web.1]: at Object.Module._extensions..js (module.js:579:10)
2022-08-05T11:57:52.809424+00:00 app[web.1]: at Module.load (module.js:487:32)
2022-08-05T11:57:52.809424+00:00 app[web.1]: at tryModuleLoad (module.js:446:12)
2022-08-05T11:57:52.809425+00:00 app[web.1]: at Function.Module._load (module.js:438:3)
2022-08-05T11:57:52.809425+00:00 app[web.1]: at Module.require (module.js:497:17)
2022-08-05T11:57:52.809425+00:00 app[web.1]: at require (internal/module.js:20:19)
2022-08-05T11:57:52.815363+00:00 app[web.1]: at Object.<anonymous> (/app/node_modules/connect-mongo/build/main/index.js:5:38)
2022-08-05T11:57:52.822611+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2022-08-05T11:57:52.822966+00:00 app[web.1]: npm ERR! errno 1
2022-08-05T11:57:52.826142+00:00 app[web.1]: npm ERR! backend@1.0.0 start: `node server.js`
2022-08-05T11:57:52.826182+00:00 app[web.1]: npm ERR! 退出状态 1
2022-08-05T11:57:52.826329+00:00 app[web.1]: npm ERR!
2022-08-05T11:57:52.826388+00:00 app[web.1]: npm ERR! backend@1.0.0 start 脚本执行失败。
2022-08-05T11:57:52.826457+00:00 app[web.1]: npm ERR! 这可能不是npm的问题

报错相关代码(MongoStore.js)

class MongoStore extends session.Store {
    constructor({ collectionName = 'sessions', ttl = 1209600, mongoOptions = {}, autoRemove = 'native', autoRemoveInterval = 10, touchAfter = 0, stringify = true, crypto, ...required }) {
        super();
        this.crypto = null;
        debug('create MongoStore instance');
        const options = {
            collectionName,
            ttl,
            mongoOptions,
            autoRemove,
            autoRemoveInterval,
            touchAfter,
            stringify,
            crypto: {
                ...{
                    secret: false,
                    algorithm: 'aes-256-gcm',
                    hashing: 'sha512',
                    encodeas: 'base64',
                    key_size: 32,
                    iv_size: 16,
                    at_size: 16,
                },
                ...crypto,
            },
            ...required,
        };

错误原因

这个报错是因为Heroku环境使用的Node.js版本过低,不支持对象解构中的剩余参数(...required)语法。该语法在Node.js 8.0.0及以上版本才被支持,而Heroku默认的Node版本可能低于这个要求;另外,新版本的connect-mongo依赖要求更高的Node版本(比如v4.x及以上需要Node.js 12+),如果你的依赖版本过高,也会触发这个问题。

解决方案

  1. 指定Node.js版本
    在项目根目录的package.json中添加engines字段,明确指定兼容的Node版本(建议和本地开发环境一致,比如14.x或更高):
"engines": {
  "node": ">=14.x"
}

提交修改后重新部署到Heroku,Heroku会按照指定版本安装Node。

  1. 调整connect-mongo版本
    如果不想升级Node版本,可以降级connect-mongo到兼容旧Node版本的分支:
  • 运行命令:npm install connect-mongo@3.x --save
  • 3.x版本的connect-mongo支持Node.js 8+,可以避免该语法错误。
  1. 确认Heroku的Node版本
    可以通过命令查看当前Heroku环境的Node版本:
heroku run node -v

确保该版本和你指定的或本地开发环境的版本一致。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 10:24:05