无法在Heroku中读取环境变量

时间:2019-02-22 09:53:31

标签: angular typescript heroku deployment

我正在尝试将应用程序部署到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的答案,但是在任何地方都找不到引用的模块。我真的不知道在哪里看,所以将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用app.json文件(doc)。 当您要将应用程序部署到Heroku时,将读取app.json并设置env变量。此外,您可以指定要执行的构建,附加组件(例如postgres)和后脚本命令。 这是您的项目的示例:

    {
       "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。 希望能有所帮助;)