【问题标题】:Heroku - Error: ENOENT: no such file or directory, stat '/app/dist/index.html'Heroku - 错误:ENOENT:没有这样的文件或目录,stat '/app/dist/index.html'
【发布时间】:2019-05-08 00:52:56
【问题描述】:

我正在尝试在 heroku 上部署我的 Angular 应用,但出现以下错误:

项目结构:

我的index.htmlsrc 文件夹中

Heroku 日志:

2018-12-06T10:30:46.837610+00:00 heroku[web.1]: Starting process with command `npm start`
2018-12-06T10:30:50.434505+00:00 app[web.1]: 
2018-12-06T10:30:50.434529+00:00 app[web.1]: > music-app@0.0.0 start /app
2018-12-06T10:30:50.434531+00:00 app[web.1]: > node server.js
2018-12-06T10:30:50.434532+00:00 app[web.1]: 
2018-12-06T10:30:51.276734+00:00 heroku[web.1]: State changed from starting to up
2018-12-06T10:30:51.217887+00:00 app[web.1]: Server started running..
2018-12-06T10:30:51.000000+00:00 app[api]: Build succeeded
2018-12-06T10:31:05.060270+00:00 heroku[router]: at=info method=GET path="/" host=boulder-app.herokuapp.com request_id=69ed1cd2-fb68-499a-9568-bfb3af482188 fwd="115.112.65.146" dyno=web.1 connect=1ms service=58ms status=404 bytes=380 protocol=https
2018-12-06T10:31:05.049971+00:00 app[web.1]:  Fetching from../app/dist/index.html
2018-12-06T10:31:05.060851+00:00 app[web.1]: Error: ENOENT: no such file or directory, stat '/app/dist/index.html'

我的 Package.json:

{
  "name": "music-app",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "node server.js",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "postinstall": "ng build --aot -prod"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^6.0.3",
    "@angular/common": "^6.0.3",
    "@angular/compiler": "^6.0.3",
    "@angular/core": "^6.0.3",
    "@angular/forms": "^6.0.3",
    "@angular/http": "^6.0.3",
    "@angular/platform-browser": "^6.0.3",
    "@angular/platform-browser-dynamic": "^6.0.3",
    "@angular/router": "^6.0.3",
    "bootstrap": "^4.1.2",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "zone.js": "^0.8.26",
    "@angular/cli": "~6.0.8",
    "@angular/compiler-cli": "^6.0.3",
    "typescript": "~2.7.2",
    "express":"^4.15.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.6.8",
    "@angular/language-service": "^6.0.3",
    "@types/jasmine": "~2.8.6",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "~8.9.4",
    "codelyzer": "~4.2.1",
    "jasmine-core": "~2.99.1",
    "jasmine-spec-reporter": "~4.2.1",
    "karma": "~1.7.1",
    "karma-chrome-launcher": "~2.2.0",
    "karma-coverage-istanbul-reporter": "~2.0.0",
    "karma-jasmine": "~1.1.1",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.3.0",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1"
  },
  "engines":{
    "node":"~8.14.0",
    "npm":"~5.0.0"
  }
}

我的 server.js:

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

app.use(express.static(__dirname + '/dist'));
app.listen(process.env.PORT || 8080);

//PATH LOCATION STARTEGY

app.get('/*', function(req,res){
  const fullPath = path.join(__dirname + '/dist/index.html');
  console.log(" Fetching from.." + fullPath);
    res.sendFile(fullPath);
})

console.log('Server started running..');

//Changed to run on Heroku

【问题讨论】:

  • 是构建应用后部署应用后显示的错误吗?
  • 您也指向 dist 文件夹,因此请确保您已使用 ng build --prod 构建了 Angular 应用程序
  • 在为生产构建 Angular 应用程序后,您会发现 dist 文件现在将您的缩小项目文件放在带有您的项目名称的文件夹中,因此您必须进行更改 const fullPath = path.join(__dirname + '/dist/project_folder/index.html');
  • @JoelJoseph :我没有看到任何错误,我收到了成功消息。检查问题正文中的 Heroku 日志。
  • 另外,我使用"postinstall": "ng build --aot -prod" 仍然没有看到创建任何dist 文件夹

标签: node.js angular heroku


【解决方案1】:

得到答案:

在 server.js 中,我必须将路径从 /dist 更改为 /dist/music-app 并且 从/dist/index.html/dist/music-app/index.html

其中,music-app 是我的 package.json 文件中的名称属性 ["name": "music-app"]。

【讨论】:

  • 更准确地说,app-nameangular.json 文件中的名称属性 "outputPath": "dist/app-name"。仅在 package.json 文件中更改 name 并像错误的构建路径一样使用它后,我遇到了同样的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 2021-09-22
  • 2021-04-26
  • 2021-04-04
  • 1970-01-01
  • 2020-09-14
  • 2020-06-03
相关资源
最近更新 更多