【问题标题】:Problem deploying Angular + Node with Heroku使用 Heroku 部署 Angular + Node 时出现问题
【发布时间】:2021-12-16 13:13:32
【问题描述】:

我很难用 heroku 部署我的 node.js(TS)/angular 应用程序。

当我尝试使用 git push heroku master 在 git 上部署它时,每一步都很好,直到 heroku-postbuild 一个。

因此,它返回以下内容:

Generating browser application bundles (phase: setup)...
remote: An unhandled exception occurred: ENOENT: no such file or directory, lstat '/tmp/build_24879b07/front/node_modules'
remote: See "/tmp/ng-REYivA/angular-errors.log" for further details.

我的项目架构是……

Project
|
|-- api/...
|-- front/...
|-- package.json
|-- ...

...这可能会导致一些问题。

此外,以下是最可能关注的文件:

./package.json

{
  "name": "mymusicads",
  "scripts": {
    "heroku-postbuild": "cd front && ng build --configuration production",
    "start": "cd api && npm run start"
  },
  "engines": {
    "node": "16.x",
    "npm": "7.x"
  },
  "dependencies": {
    "@angular-devkit/build-angular": "^12.2.12",
    "@angular/cli": "^11.2.13",
    "@angular/common": "^11.2.13",
    "@angular/compiler": "^12.2.12",
    "@angular/compiler-cli": "^11.2.13",
    "@angular/core": "^11.2.13",
    "@angular/platform-browser": "^11.2.13",
    "rxjs": "^6.6.7",
    "typescript": "^4.3.5"
  }
}

./api/index.ts

import express, { Request, Response } from 'express';
import * as dotenv from 'dotenv';
import path from 'path';
import { adDataRoutes } from './app/routes/ad-data.route';
import { errorHandler } from './middleware/error.middleware';
import { notFoundHandler } from './middleware/not-found.middleware';

dotenv.config();

const app = express();

app.use(express.json());
app.use('/api/addata', adDataRoutes);
app.use(express.static(path.join(__dirname, 'mymusicads', 'dist', 'front')));

app.use(errorHandler);
app.use(notFoundHandler);

app.get('/', (req: Request, res: Response) => {
  res.sendFile(path.join(__dirname, 'mymusicads', 'dist', 'front', 'index.html'));
});

app.listen(process.env.PORT || 3000, () => {
  console.log(`Listening on port ${process.env.PORT || 3000}`);
});

./front/angular.json

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "front": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "style": "scss"
        },
        "@schematics/angular:application": {
          "strict": true
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/front",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "styles": [
              "./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
              "src/styles.scss"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "3mb",
                  "maximumError": "6mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "10kb",
                  "maximumError": "20kb"
                }
              ]
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "front:build",
            "proxyConfig": "proxy.conf.json"
          },
          "configurations": {
            "production": {
              "browserTarget": "front:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "front:build"
          }
        },
        "test": { [...] },
        "lint": { [...] }
        }
      }
    }
  },
  "defaultProject": "front"
}

据我了解,问题来自我的api/index.ts 文件,我可能没有使用res.sendFile(path.join(__dirname, 'mymusicads', 'dist', 'front', 'index.html')) 定位正确的路径。

谢谢!

【问题讨论】:

    标签: node.js angular heroku


    【解决方案1】:

    不记得我是否遇到过同样的问题,但设置不同。在front 文件夹中,我有一个专门用于 Angular 应用程序的 package.json - 基本上是默认的 Angular CLI 设置

    顶级package.json 几乎没有依赖项(如果有的话),主要是脚本(您在前面使用的客户端文件夹)。对于预构建,在客户端文件夹中运行 npm install,然后运行 ​​npm run --prefix client build -- -- prod

    对于服务器:

    // Static files
    app.use(
        express.static(path.join(__dirname, '../client/dist/my-app'), {
            maxAge: '1y',
        })
    );
    // Angular app
    app.get('*', (req, res) => {
        res.sendFile(
            path.join(__dirname, '../client/dist/my-app/index.html')
        );
    });
    

    【讨论】:

    • 你好 Drenai,这很有趣!我认为您在这里提出了一些重要的问题,我的静态文件夹可能不存在。我更改了它的路径以匹配我的应用程序(例如__dirname/../../front/dist/front),但我实际上意识到问题可能出在其他地方。错误是ENOENT: no such file or directory, lstat '/tmp/build_24879b07/front/node_modules' ,看来heroku 在我的前端应用程序中没有找到node_modules 文件夹...我忘记了什么吗?
    • 您可能错过了预构建步骤(添加了详细信息)
    • 我想就是这样!我在前面的 package.json 中的构建指令之前添加了npm install &&,它似乎已经解锁了这种情况。我会尽快继续调查这个问题,另一个错误是关于 @angular/compiler-cli 的错误版本。
    • 啊酷,不错?
    • 确实,我忘记了预构建阶段!但仔细检查首先提到的静态目标肯定避免了我在预构建后的一些错误。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2019-02-18
    • 2021-04-09
    • 2021-05-09
    • 1970-01-01
    • 2021-12-31
    • 2015-02-19
    相关资源
    最近更新 更多