node.js Heroku ENOENT:没有这样的文件或目录,stat '/app/build/index.html'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39535558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 20:33:30  来源:igfitidea点击:

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

node.jsheroku

提问by screenm0nkey

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/.

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

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

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

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

在这里读到不要在快递应用程序中使用 __dirname ,因为 Heroku 将其设置为 /app 并且我应该使用 process.cwd();

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

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

但它没有用。下面是我的 server.js express 应用程序和文件夹结构

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');

directory structure

目录结构

回答by hunterloftis

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.

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

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.

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

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).

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

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

回答by jasharbn

distfolder is ignored. therefore heroku returns this error. When you list your application files in heroku, you can see it is missing.

dist文件夹被忽略。因此 heroku 返回此错误。当您在 heroku 中列出您的应用程序文件时,您会发现它丢失了。

How to list folder/files in heroku

如何在heroku中列出文件夹/文件

  1. run heroku run bashin command line inside of your app folder
  2. type dirand enter. then you will see the list of folders and files
  1. 在应用程序文件夹内的命令行中运行heroku run bash
  2. 输入dir并回车。然后你会看到文件夹和文件的列表

回答by Yakup Ad

Check your package.json file

检查您的 package.json 文件

"postinstall": "ng build --aot --prod"

Check your server.js file

检查你的 server.js 文件

const express = require('express');
const path = require('path');
const app = express();

app.use(express.static(__dirname + '/dist/YOURPROJECTNAME'));
app.get('/*', function(req,res) {
res.sendFile(path.join(__dirname + '/dist/YOURPROJECTNAME/index.html'));

});

app.listen(process.env.PORT || 8080);

回答by Dave Thomas

Once you add the static file location with:

添加静态文件位置后:

app.use(express.static('build'));

Express looks up the files relative to the static directory, so the name of the static directory is not part of the URL. You should be able to just do this to get index.html:

Express 查找相对于静态目录的文件,因此静态目录的名称不是 URL 的一部分。你应该能够做到这一点来获取 index.html:

app.get('*', function (req, res) {
  res.sendFile('index.html');
});

See https://expressjs.com/en/starter/static-files.html

https://expressjs.com/en/starter/static-files.html

回答by immirza

I had similar issue where Heroku openkept on complaining for missing index.htmlinside /app. However it was working fine locally 'Heroku Local'.

我有类似的问题,Heroku open一直抱怨在index.html里面失踪/app。但是,它在本地“ Heroku Local”运行良好。

The issue was i had explicitly ignored compiled files/folder e.g. /distin the gitignorefile and hence /distwas never pushed onto the herokudeployed folder.

这个问题是我已经明确地忽略编译后的文件/文件夹,例如/distgitignore的文件,因此/dist从来没有推到heroku部署的文件夹。

Do,

做,

heroku git bash
dir

and ensure you have dist folder (with compiled files)listed here and all should be fine.

并确保您已dist folder (with compiled files)在此处列出并且一切正常。

回答by Giordi Femi Alba

I had the same issue but I had set everything. Heroku Post build inside my node package.json file. See below

我有同样的问题,但我已经设置了一切。Heroku Post 在我的节点 package.json 文件中构建。见下文

"scripts": {
  "start": "node index.js",
  "heroku-postbuild": "cd client && npm install && npm run build"
}
  • Was still getting the error Error: ENOENT: no such file or directory, stat + app/client/build/index.html'
  • 仍然收到错误错误:ENOENT:没有这样的文件或目录,stat + app/client/build/index.html'

Just found out that this was happening because in my react folder index.js file Please see below

刚刚发现这是因为在我的反应文件夹 index.js 文件中,请参见下文

import React from 'react';
import ReactDom from 'react-dom'
import App from './App' #importing the App.js component

ReactDom.render(<App/>, document.getElementById('root')) 
// render the react app

However I was not exporting my App.js component, hence, when I deployed to heroku the file was not being picked up.

但是我没有导出我的 App.js 组件,因此,当我部署到 heroku 时,文件没有被提取。

Here is the line to export my App component

这是导出我的 App 组件的行

export default App;
  • Hope this helps someone.
  • 希望这可以帮助某人。

回答by user1449456

Note that it's a better practice to use heroku-postbuildcommand in your script rather than pushing the buildfolder.

请注意,heroku-postbuild在脚本中使用命令而不是推送build文件夹是更好的做法。

"scripts": {
  "start": "node index.js",
  "heroku-postbuild": "cd client && npm install && npm run build"
}