Skip to content

Environment Variables

Antony Budianto edited this page Jul 27, 2016 · 47 revisions

As a developer who works with many environments, like local machine, your teammate's machine, staging, QA, or production, you need to make sure that changing settings won't breaks other environments. The usual way is to have environment variables which varies between environments.

The usual use cases:

  • Staging server uses stag-api.domain.com where Production server uses api.domain.com
  • My local machine uses localhost where my teammate's machine uses api.domain.dev
  • PORT differences

The starter supports file-based and strong-typed environment variables

❔ How it works

It works by reading your env.json located at project root and transforming it into a TypeScript file that exports a const so you can import it into your application.

ℹ️ How to use

  1. Define your app env interface in src/app/shared/constant/env.model.ts
  2. Create env.json file at project root and add your env key-value pairs there.
    • The key must matches the env interface defined in step 1, unless it's an optional key
  3. Now, every transpilation will generate a new src/app/shared/constant/env.ts and you can import it to your application
  4. You also can generate it manually by running npm run env

💡 Tips

  • You can directly update generated src/app/shared/constant/env.ts file for fast update and response in development
  • Store the production version of env.json on server and copy it to the project root before npm run build
  • When you update AppEnv interface, you can update env.example.json so others can use as quick template for their environment
  • Both env.json and src/app/shared/constant/env.ts are git-ignored so changing them won't affect other environments.

⚠️ Warning

  • Since this is client-side environment variables, DON'T STORE SENSITIVE INFORMATION like your api tokens, secret keys, or other variables that should be kept secret on server-side

Environment variable feature is optional, you can do so by emptying the AppEnv interface so you don't need to have env.json at all

☀️ Support this starter by sharing it to your friends, giving stars, or pull requests! 😄

Clone this wiki locally