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

cPanel部署Node.js应用报错:expected ( saw {,求解决方案

cPanel部署Node.js应用报错"expected ( saw {"及/api/user路由失效解决方案

问题场景

在cPanel部署Node.js应用时遇到以下问题:

  • 系统抛出语法错误:expected ( saw {
  • /api路由可正常访问,但/api/user路由无法工作
  • 本地使用Node.js 18.15.0运行完全正常,cPanel提供的可用Node.js版本为<16.20.1和18.16.0
  • 伴随async/await相关运行时错误(表现为请求无响应或服务崩溃)

相关核心代码:

const express = require('express');
const app = express();
const port = process.env.PORT;
const { User, Home } = require("./models")
const cors = require("cors");

app.use(express.json())
const { sequelize } = require("./models")

async function main() {
    await sequelize.sync({});
}

// main();

app.use(
    cors({
        origin: "*",
    })
);
app.get("/api", (req, res) => {
    res.status(200).json({
        "message": "API enpoints for first app"
    });
});

const userRouter = require("./routes/user");
const homeRouter = require("./routes/home");
const bodyParser = require('body-parser');

// app.use("/user", userRouter);
app.use("/home", homeRouter);

app.get("/api/user", async (req, res) => {
    const users = await User.findAll(
        {
            include: [
                {
                    model: Home, as: "homes",
                },
            ]
        }
    );

    res.json({
        "message": "success",
        "data": users
    });
});
app.get('/api/node-version', (req, res) => {
  res.send(process.version);
});

app.listen(port, () => {
    console.log("yo yo let's goo");
});

问题原因

  1. 尾随逗号(Trailing Commas)兼容性问题:部分旧版Node.js(如16.x早期版本)不允许对象或数组最后一项后添加尾随逗号,而代码中多处使用了这类逗号,触发语法错误expected ( saw {。
  2. async函数未处理错误:/api/user路由的async函数未添加错误捕获逻辑,一旦sequelize查询抛出异常,会导致未捕获的Promise rejection,进而引发服务崩溃或请求挂起。
  3. 版本差异适配问题:本地18.15.0对新语法支持更完善,而cPanel中的旧版Node.js可能存在语法或依赖兼容问题。

解决步骤

1. 移除代码中的尾随逗号

清理所有对象、数组末尾的多余逗号,修改后关键代码片段:

// 修正cors配置
app.use(
    cors({
        origin: "*"
    })
);

// 修正User.findAll查询配置
app.get("/api/user", async (req, res) => {
    const users = await User.findAll({
        include: [
            {
                model: Home, as: "homes"
            }
        ]
    });

    // ...
});

2. 为async路由添加错误捕获

给/api/user路由添加try/catch块,捕获并处理查询异常:

app.get("/api/user", async (req, res) => {
    try {
        const users = await User.findAll({
            include: [
                {
                    model: Home, as: "homes"
                }
            ]
        });

        res.json({
            "message": "success",
            "data": users
        });
    } catch (err) {
        console.error("用户查询失败:", err);
        res.status(500).json({
            "message": "获取用户列表失败",
            "error": err.message
        });
    }
});

3. 选择匹配的Node.js版本

优先选择cPanel中的18.16.0版本,该版本与本地18.15.0几乎无差异,能最大程度避免版本兼容问题。若必须使用16.x版本,选择最高的16.20.1,它对现代语法的支持更完善。

4. 校验依赖兼容性

确保sequelize等核心依赖与所选Node.js版本兼容,建议升级sequelize到最新稳定版:

npm install sequelize@latest

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.15 03:53:20