使用 Heroku 部署 Angular + 节点时出现问题

Problem deploying Angular + Node with Heroku

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

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

因此,returns 如下:

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')) 定位正确的路径。

谢谢!

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

顶级 package.json 几乎没有依赖项(如果有的话),主要是脚本(您使用 front 的客户端文件夹)。对于预构建,客户端文件夹中的 运行 npm installnpm 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')
    );
});