【问题标题】:Heroku Error 404, "ENOENT: no such file or directory, stat '/app/client/build/index.html'"Heroku 错误 404,“ENOENT:没有这样的文件或目录,stat '/app/client/build/index.html'”
【发布时间】:2021-09-22 11:23:42
【问题描述】:

我正在尝试在 heroku 中部署 MERN 网站,但出现以下错误:

"ENOENT: no such file or directory, stat '/app/client/build/index.html'"

我已在本地运行该项目,一切正常。 我在 package.json 中添加了以下内容:

"heoku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"

这也是我的 server.js

中的代码
// Deployment
if (process.env.NODE_ENV === "production") {
  app.use(express.static(path.join(__dirname, "../client/build")));
  app.get("*", (req, res) =>
    // res.sendFile(path.resolve(__dirname, "../client", "public", "index.html"))
    res.sendFile(path.resolve(__dirname, "../client/build/index.html"))
  );
} else {
  app.get("/", (req, res) => {
    res.send("API IS RUNNING.");
  });
}

它应该通过运行 npm run build 创建 react build 文件夹,但我认为 heroku-postbuildcommand 不起作用。

【问题讨论】:

    标签: node.js reactjs heroku create-react-app


    【解决方案1】:

    我将heroku 拼错为heoku,所以 package.json 中的实际命令应该是:

    "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-24
      • 2021-04-04
      • 2019-05-31
      • 2020-12-05
      • 2019-05-08
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      相关资源
      最近更新 更多