Heroku ENOENT:没有这样的文件或目录,stat '/app/build/index.html' [英] Heroku ENOENT: no such file or directory, stat '/app/build/index.html'

查看:23
本文介绍了Heroku ENOENT:没有这样的文件或目录,stat '/app/build/index.html'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Heroku 中运行一个简单的测试站点时遇到问题.出于某种原因,当我希望从/build/提供内容时,它会尝试从/app/build/提供内容.

错误:ENOENT:没有那个文件或目录,stat '/app/build/index.html'

我读了

解决方案

首先,不要使用 PWD.只需使用 __dirname.它在 Heroku 上的工作方式与在其他任何地方的工作方式完全一样.例如,如果您从非本地目录执行二进制文件,则使用 PWD 会使您的应用更加脆弱.

其次,该文件在 Heroku 上不存在的原因可能是因为您已将其添加到 .gitignore 文件中或通常未将其检入 git.您可以从编辑器的颜色编码中看到这一点 - 所有签入 git 的文件都是白色的,被忽略的文件是灰色的(如 node_modules 目录和 build/bundle).您的索引文件是红色的(不像签入的文件是白色的).所以当你 git push heroku master 时,你引用的那些文件不存在.

最后,ENOENT 之所以说/app/build 只是因为您在 Heroku 上的根目录/主目录是/app.永远不要构建锁定在绝对文件结构中的应用程序;只需使用相对路径(否则,即使将其移动到本地计算机上的另一个目录,您的应用程序也会中断).

app.get('*', function (req, res) {const index = path.join(__dirname, 'build', 'index.html');res.sendFile(index);});

I'm having trouble running a simple test site in Heroku. For some reason it's trying to serve the content from /app/build/ when I want it to serve from /build/.

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

I read here not to use __dirname in the express app as Heroku set it to /app and that I should use process.cwd();

process.env.PWD = process.cwd();

But it didn't work. Below is my server.js express app and folder structure

const path = require('path');
const express = require('express');
const port = process.env.PORT || 8080;
const app = express();

process.env.PWD = process.cwd();

app.use(express.static(process.env.PWD + '/build'));

app.get('*', function (req, res) {
  const index = path.join(process.env.PWD, '/build/index.html');
  res.sendFile(index);
});

app.listen(port);
console.log('server_started');

解决方案

First, don't use PWD. Just use __dirname. It works on Heroku exactly as it works anywhere else. Using PWD makes your app more brittle if you, for instance, execute a binary from a non-local directory.

Second, the reason that file doesn't exist on Heroku is likely because you've added it to your .gitignore file or generally haven't checked it into git. You can see this from the color coding of your editor - all of the files checked into git are white, the ones ignored are gray (like the node_modules directory and build/bundle). Your index file is red (not white like the checked-in files). So when you git push heroku master, those files you're referencing don't exist.

Finally, the reason ENOENT says /app/build is just because your root/home directory on Heroku is /app. Never build apps that are locked into an absolute file structure; just use relative paths (otherwise, your app will break if you even move it to another directory on your local machine).

app.get('*', function (req, res) {
  const index = path.join(__dirname, 'build', 'index.html');
  res.sendFile(index);
});

这篇关于Heroku ENOENT:没有这样的文件或目录,stat '/app/build/index.html'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆