【问题标题】:Can't read environment variables in HerokuHeroku 无法读取环境变量
【发布时间】:2019-07-16 09:21:13
【问题描述】:

我正在尝试将应用程序部署到 Heroku。它正在正确构建(使用 heroku nodejs buildpack),但我无法访问 Heroku 的环境变量。这是正在使用的环境文件:

import * as process from 'process'

console.log(process); // Object containing numerous props (cwd, chdir, browser, etc.)
console.log(process.env); // Empty object but NOT undefined
console.log(process.env.BACKEND_URL); // Undefined

export const environment = {
    production: true,
    baseUrl: process.env.BACKEND_URL,
    apiBaseUrl: process.env.BACKEND_URL + '/api/v1/',
    cmsBaseUrl:  process.env.BACKEND_URL + '/content/pages/',
    verboseApiCalls: false
};

正在运行的构建命令:
"postinstall": "ng build --aot --prod && npm install -g serve",

Angular.json:

{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "newProjectRoot": "projects",
  "projects": {
    "front-end": {
      "root": "",
      "sourceRoot": "src",
      "projectType": "application",
      "prefix": "app",
      "schematics": {},
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "dist/front-end",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.app.json",
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ],
            "stylePreprocessorOptions": {
              "includePaths": [
                "src/assets/styles/partials"
              ]
            },
            "styles": [
              "src/styles-app-loading.scss",
              "src/assets/styles/catch.scss",
              "src/styles.css"
            ],
            "scripts": []
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.prod.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": true,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true
            },
            "local": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.local.ts"
                }
              ],
              "optimization": false,
              "outputHashing": "all",
              "sourceMap": false,
              "extractCss": true,
              "namedChunks": false,
              "aot": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": false
            }
          }
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "front-end:build",
            "disableHostCheck": true
          },
          "configurations": {
            "production": {
              "browserTarget": "front-end:build:production"
            },
            "local": {
              "browserTarget": "front-end:build:local"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "front-end:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "src/tsconfig.spec.json",
            "karmaConfig": "src/karma.conf.js",
            "styles": [
              "src/styles.css"
            ],
            "stylePreprocessorOptions": {
              "includePaths": [
                "src/assets/styles/partials"
              ]
            },
            "scripts": [],
            "assets": [
              "src/favicon.ico",
              "src/assets"
            ]
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": [
              "src/tsconfig.app.json",
              "src/tsconfig.spec.json"
            ],
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    },
    "front-end-e2e": {
      "root": "e2e/",
      "projectType": "application",
      "architect": {
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "front-end:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "front-end:serve:production"
            }
          }
        },
        "lint": {
          "builder": "@angular-devkit/build-angular:tslint",
          "options": {
            "tsConfig": "e2e/tsconfig.e2e.json",
            "exclude": [
              "**/node_modules/**"
            ]
          }
        }
      }
    }
  },
  "defaultProject": "front-end"
}

Package.json:(不知道是否相关)

{
  "name": "front-end",
  "version": "0.1.0",
  "scripts": {
    "ng": "ng",
    "build": "ng build",
    "test": "ng test",
    "postinstall": "ng build --aot --prod && npm install -g serve",
    "start": "serve dist/front-end/ -l $PORT",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^7.1.3",
    "@angular/cdk": "^7.1.1",
    "@angular/cli": "~7.1.2",
    "@angular/common": "^7.1.3",
    "@angular/compiler": "^7.1.3",
    "@angular/core": "^7.1.3",
    "@angular/flex-layout": "^7.0.0-beta.21",
    "@angular/forms": "^7.1.3",
    "@angular/http": "^7.1.3",
    "@angular/material": "^7.1.1",
    "@angular/platform-browser": "^7.1.3",
    "@angular/platform-browser-dynamic": "^7.1.3",
    "@angular/router": "^7.1.3",
    "@swimlane/ngx-charts": "^10.0.0",
    "classlist.js": "^1.1.20150312",
    "core-js": "^2.5.4",
    "rxjs": "^6.0.0",
    "typescript": "~3.1.6",
    "zone.js": "^0.8.26"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^0.13.2",
    "@angular/cli": "~7.1.2",
    "@angular/compiler-cli": "^7.2.5",
    "@angular/language-service": "^7.1.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",
    "karma-spec-reporter": "0.0.32",
    "protractor": "^5.4.1",
    "ts-node": "~5.0.1",
    "tslint": "~5.9.1",
    "typescript": "~3.1.6"
  },
  "engines": {
    "node": "10.3.0"
  }
}

我尝试了this 的答案,但在任何地方都找不到引用的模块。我真的不知道在哪里看,所以任何帮助将不胜感激。

【问题讨论】:

    标签: angular typescript heroku deployment


    【解决方案1】:

    您可以使用 app.json 文件 (doc)。 当您想将应用程序部署到 Heroku 时,会读取 app.json 并设置环境变量。此外,您可以指定要执行的构建、插件(例如 postgres)和 post-script 命令。 这是您项目的示例:

        {
           "name": "Example",
           "description": "This app does one little thing",
           "success_url": "/",
           "scripts": {
              "postdeploy": "database create && database update"
             },
          "env": {
            "production": "true",
            "verboseApiCalls": "false"
          }
      }
    

    还有一步 :),我将 app.json 推送到我的远程存储库 (github) 上,并使用 following url 来部署应用程序:

    https://heroku.com/deploy?template=https://github.com/yourNameOrOrganisation/yourRepository
    

    别忘了还要添加一个 Procfile。 希望对您有所帮助;)

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 2021-10-28
      • 2013-06-07
      • 2020-01-27
      • 1970-01-01
      相关资源
      最近更新 更多