-
Notifications
You must be signed in to change notification settings - Fork 142
Environment Variables
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.comwhere Production server usesapi.domain.com - My local machine uses
localhostwhere my teammate's machine usesapi.domain.dev - PORT differences
The starter supports file-based and strong-typed environment variables
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.
-
Define your app env interface in
src/app/shared/constant/env.model.ts- The key can be optional
- The key types must be valid JSON data types
-
Create
env.jsonfile 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
- Now, every transpilation will generate a new
src/app/shared/constant/env.tsand you can import it to your application- You also can generate it manually by running
npm run env
- You also can generate it manually by running
-
Access the env in
CONSTANT.ENVfromsrc/app/sharedmodule
- You can directly update generated
src/app/shared/constant/env.tsfile for fast update and response in development. Of course you need to updateenv.jsonfor permanent update - Bundling
- It's up-to-you to commit production/staging/etc json files like
env.production.jsoninto source control OR to keep it somewhere in the server/machine. First choice is favorable when you have limited/no server access - Before bundling using
npm run build, copyenv.jsonfrom server/repo
- It's up-to-you to commit production/staging/etc json files like
- When you update
AppEnvinterface, you can updateenv.example.jsonalong so others can use as quick template for their environment
- 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
- This is why you can commit
env.<environment>.jsonto source control since it should only contains non-sensitive environment values.
Environment variable feature is optional, you can do so by emptying the AppEnv interface so you don't need to have
env.jsonat all
This starter may not fit for your workflow, since it's opinionated. Therefore you can always fork and custom it to fit your workflow
☀️ Support this starter by sharing it to your friends, giving stars, or pull requests! 😄