【问题标题】:Heroku not finding express in server.jsHeroku 在 server.js 中找不到 express
【发布时间】:2021-10-27 15:40:34
【问题描述】:

在将我的 react 应用程序部署到 Heroku 时遇到困难。我收到以下错误:

ReferenceError: express is not defined

这里是项目结构:

  • 后端文件夹
  • 前端文件夹
  • package.json
  • 过程文件

这是我的 Procfile

web: node backend/server.js

这是根目录下的 package.json

  "name": "zippora",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "start": "node backend/server.js",
    "heroku-postbuild": "cd frontend && npm install && npm run build"
  },
  "engines": {
    "node": "16.4.1",
    "npm": "7.18.1"
  },
  "dependencies": {
    "bcrypt": "^5.0.0",
    "body-parser": "^1.19.0",
    "colors": "^1.4.0",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^5.0.0-alpha.8",
    "jsonschema": "^1.4.0",
    "jsonwebtoken": "^8.5.1",
    "morgan": "^1.10.0",
    "pg": "^8.3.0"
  }
}

后端文件夹中的我的 server.js 文件


const app = require("./app.js");
const { PORT } = require("./config");
app.use(express.static("../frontend/build/index.html"))
app.get("/", (res,req) => {
  res.send({"message":"Hello?"})
})
app.listen(PORT, function () {
  console.log(`Started on http://localhost:${PORT}`);
});

我的 app.js 文件在后端的顶部


/** Express app for zippora. */

const express = require("express");
const cors = require("cors");

const { NotFoundError } = require("./expressError");

const { authenticateJWT } = require("./middleware/auth");
const authRoutes = require("./routes/auth");
const usersRoutes = require("./routes/users");

const morgan = require("morgan");

const app = express();```

【问题讨论】:

    标签: reactjs express heroku


    【解决方案1】:

    请在 server.js 中定义 express

    const express = require('express');
    

    您的 Server.js 代码变为 ...

    const express = require('express');
    const app = require("./app.js");
    const { PORT } = require("./config");
    app.use(express.static("../frontend/build/index.html"))
    app.get("/", (res,req) => {
      res.send({"message":"Hello?"})
    })
    app.listen(PORT, function () {
      console.log(`Started on http://localhost:${PORT}`);
    });
    

    【讨论】:

    • 我马上试试,谢谢
    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    • 回答了这个具体的错误,谢谢!继续下一期
    猜你喜欢
    • 1970-01-01
    • 2018-11-07
    • 2019-11-18
    • 1970-01-01
    • 2013-03-22
    • 2017-02-23
    • 2016-05-17
    • 1970-01-01
    • 2020-02-28
    相关资源
    最近更新 更多