【问题标题】:Heroku no such file or directory, stat '/app/client/build/index.html'Heroku 没有这样的文件或目录,stat '/app/client/build/index.html'
【发布时间】:2019-05-31 09:47:25
【问题描述】:

我已经为这个错误苦苦挣扎了将近三个星期,老实说,这让我发疯了。一年多来,我一直在使用 Heroku 部署我的项目,在我要发布我的这个新网站之前,我从未遇到任何错误。你看,我目前在我的节点项目中安装了一个邮件服务器,名为“index.js”,而我的完整 React 项目位于一个名为 client 的文件夹中。

现在,这很奇怪。我的 index.js 看起来像这样:

if (process.env.NODE_ENV === 'production') {
    app.use(express.static('client/build'));
    const path = require('path');
    app.get('*', (req, res) => {
      res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
    });
  }

但是每次我推送到 Heroku 时,我都会在控制台中收到以下错误消息:

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

我也一直在尝试更改和修改路径中的目录,看看是否有任何变化。我还浏览了整个互联网关于这个问题的潜在解决方案的感觉,但没有任何运气。如果有人至少能指出我在这里似乎做错了什么的正确方向,我将不胜感激。

提前致谢。

【问题讨论】:

  • 你能发布你的package.json吗?更具体地说,脚本...

标签: reactjs heroku heroku-cli


【解决方案1】:

我刚刚遇到了同样的问题。您需要在 package.json 中添加 heroku-postbuild 脚本。我在我的项目中使用了 create-react-app,所以如果你不这样做,这行可能会有所不同:

"heroku-postbuild": "cd client && npm install --only=dev && npm install && npm run build"

当我运行npm run build 时,create-react-app 会在 build/ 文件夹中编译一个缩小的 index.html 文件,因此如果您指向的构建文件位于其他位置,您可能需要修改命令。

我的服务器结构是这样的:

server.js
client/
  build/
  public/
    - index.html
  src/
    - index.js


我在this handy article找到了解决方案

【讨论】:

    【解决方案2】:

    我遇到了同样的错误,但我的问题不是上述问题,所以我决定在这里发布 - 也许可以帮助某人。
    我想部署我当前的工作分支,它不是 master,但是在运行命令 git push heroku master 时,我没有意识到默认部署的分支是 master。由于我领先于 master 一些提交,包括在新位置连接我的 server.js 文件,heroku 无法找到它。
    所以实际上我的解决方案就像运行 git push heroku <current-branch-name>:master 一样简单,现在一切正常。

    【讨论】:

      【解决方案3】:

      我在使用 NodeJS 和 Express 的 React 项目中也遇到了同样的问题。在我有 NodeJS 文件的文件夹中(而不是在 React 所在的客户端文件夹中)我在 package.json 中有这个:

      "scripts": {
          "start": "node server.js",
          "server": "nodemon server.js",
          "client": "npm start --prefix client",
          "client-install": "npm install --prefix client",
          "dev": "concurrently \"npm run server\" \"npm run client\"",
          "heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
        },
      

      在 server.js 中我有:

      // Serve static assets in production
      if (process.env.NODE_ENV === 'production') {
        // Set static folder
        app.use(express.static('client/build'));
      
        app.get('*', (request, response) => {
          response.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
        });
      }
      

      在同一个文件夹(NodeJS)中,我有一个供 Mongo 用户使用的文件 production.js 并通过:

      module.exports = {
        mongoURI:
          'mongodb+srv://yyyyyyyyy:xxxxxxxx@cluster0-pana5.mongodb.net/test?retryWrites=true&w=majority',
      };
      

      所以在 server.js 中我带来了这个文件:

      const db = require('./config/production').mongoURI;
      

      仍然在这里,使用 db 连接到 MongoDB:

      mongoose
        .connect(db, {
          useNewUrlParser: true,
          useCreateIndex: true,
          useFindAndModify: false
        })
        // use a promise to check if success
        .then(() => console.log('MongoDB Connected!'))
        .catch(error => console.log('MongoDB did not connect: ', error));
      

      对我来说是这样工作的。

      【讨论】:

        【解决方案4】:

        没有使用 Heroku,我猜问题是“/app/client/build/index.html”。

        真的在/app/下吗?也许您应该使用如下相对路径:

        app/client/build/index.html
        

        【讨论】:

        • 这就是我所相信的(不完全确定),这一行的意思是: res.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));而 __dirname 是“app”,就我而言,这是 Heroku 的根目录。
        猜你喜欢
        • 2021-09-22
        • 2017-01-24
        • 1970-01-01
        • 2020-12-05
        • 2021-04-04
        • 2019-05-08
        • 1970-01-01
        • 2021-04-26
        • 1970-01-01
        相关资源
        最近更新 更多