All Projects → chihab → ngx-env

chihab / ngx-env

Licence: other
Easily inject environment variables into your Angular applications

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to ngx-env

dart environment config
Environment specific config generator for Dart and Flutter applications during CI/CD builds
Stars: ✭ 87 (+19.18%)
Mutual labels:  dotenv, environment, configuration, environment-variables
Phpdotenv
Loads environment variables from `.env` to `getenv()`, `$_ENV` and `$_SERVER` automagically.
Stars: ✭ 11,648 (+15856.16%)
Mutual labels:  dotenv, environment, configuration, environment-variables
environment
🌳 Environment variable configuration for Node.js made easy.
Stars: ✭ 12 (-83.56%)
Mutual labels:  environment, configuration, variables
Env
Simple lib to parse environment variables to structs
Stars: ✭ 2,164 (+2864.38%)
Mutual labels:  environment, configuration, environment-variables
Environ Config
Python Application Configuration With Environment Variables
Stars: ✭ 210 (+187.67%)
Mutual labels:  environment, configuration, environment-variables
Confex
Useful helper to read and use application configuration from environment variables.
Stars: ✭ 272 (+272.6%)
Mutual labels:  system, configuration, environment-variables
envy
Use envy to manage environment variables with your OS keychain
Stars: ✭ 23 (-68.49%)
Mutual labels:  environment, environment-variables, variables
Fig
A minimalist Go configuration library
Stars: ✭ 142 (+94.52%)
Mutual labels:  environment, configuration, environment-variables
Next Runtime Dotenv
Expose environment variables to the runtime config of Next.js
Stars: ✭ 136 (+86.3%)
Mutual labels:  dotenv, environment, configuration
gconfigs
gConfigs - Config and Secret parser
Stars: ✭ 42 (-42.47%)
Mutual labels:  dotenv, configuration, environment-variables
superconfig
Access environment variables. Also includes presence validation, type coercion and default values.
Stars: ✭ 33 (-54.79%)
Mutual labels:  dotenv, configuration, environment-variables
dotenvy
Speed up your production sites by ditching .env for key/value variable pairs as Apache, Nginx, and shell equivalents
Stars: ✭ 31 (-57.53%)
Mutual labels:  dotenv, environment, environment-variables
Conf
Go package for loading program configuration from multiple sources.
Stars: ✭ 70 (-4.11%)
Mutual labels:  environment, configuration, environment-variables
ts-dotenv
Strongly-typed environment variables for Node.js
Stars: ✭ 18 (-75.34%)
Mutual labels:  dotenv, environment, environment-variables
Env Var
Verification, sanitization, and type coercion for environment variables in Node.js
Stars: ✭ 201 (+175.34%)
Mutual labels:  dotenv, environment, environment-variables
ini
📝 Go INI config management. support multi file load, data override merge. parse ENV variable, parse variable reference. Dotenv file parse and loader. INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用。DotEnv 解析加载
Stars: ✭ 72 (-1.37%)
Mutual labels:  dotenv, environment, environment-variables
paerser
No description or website provided.
Stars: ✭ 38 (-47.95%)
Mutual labels:  configuration, environment-variables
checkdotenv
Verify environment variables presence for Node JS.
Stars: ✭ 12 (-83.56%)
Mutual labels:  dotenv, environment-variables
as-a
Runs a given command with additional environment settings for simple local development
Stars: ✭ 60 (-17.81%)
Mutual labels:  environment, environment-variables
DBEnvironmentConfiguration
Easily switch between iOS development environments/ configurations
Stars: ✭ 18 (-75.34%)
Mutual labels:  environment, environment-variables

@ngx-env/builder

dotenv

npm version lerna

Easily inject environment variables into your Angular applications

Quick start

  1. Add @ngx-env to your CLI project
ng add @ngx-env/builder
  1. Define Environment Variables in .env
NG_APP_VERSION=$npm_package_version
NG_APP_COMMIT=$GITHUB_SHA
NG_APP_ENABLE_SENTRY=false
  1. Use in TS and HTML
@Component({
  selector: "app-main",
})
export class MainComponent {
  branch = process.env.NG_APP_BRANCH_NAME;
}
<span> {{ branch }} </span>
<!-- Using env pipe from @ngx-env/core -->
<span> {{ 'process.env.NG_APP_VERSION' | env }} </span>
<span> {{ 'NG_APP_COMMIT' | env }} </span>
<!-- index.html -->
<head>
  <title>NgApp on %NG_APP_BRANCH_NAME% - %NG_APP_COMMIT%</title>
</head>
  1. Run your CLI commands
npm start
NG_APP_BRANCH_NAME=$GITHUB_HEAD_REF ng test
NG_APP_ENABLE_SENTRY=true npm run build

Compatiblity ⚠️

Angular @ngx-env/builder
[12, 13] ^2.0.1
[8, 12[ ^1.1.0

Using Environment Variables

Your project can consume variables declared in your environment as if they were declared locally in your JS files.

These environment variables can be useful for:

  • displaying information conditionally based on where the project is deployed
  • consuming sensitive data that lives outside of version control.

The environment variables will be defined for you on process.env. For example, having an environment variable named NG_APP_NOT_SECRET_CODE will be exposed in your JS as process.env.NG_APP_NOT_SECRET_CODE.

The environment variables are embedded during the build time.

Important

Do not store any secrets (such as private API keys) in your Angular app!

Environment variables are embedded into the build, meaning anyone can view them by inspecting your app's files.

NG_APP_*

You must create custom environment variables beginning with NG_APP_.

Any other variables will be ignored to avoid accidentally exposing a private key on the machine that could have the same name. See how to use system environment variables.

Changing any environment variables will require you to restart the development server if it is running.

NG_APP_ENV

There is also a built-in environment variable called NG_APP_ENV. You can read it from process.env.NG_APP_ENV.

By default NG_APP_ENV is set to NODE_ENV but you are free to override it.

Having access to the NG_APP_ENV is also useful for performing actions conditionally:

if (process.env.NG_APP_ENV !== "production") {
  analytics.disable();
}

When you compile the app with npm run build, the minification step will strip out this condition, and the resulting bundle will be smaller.

Usage In Templates

You have two options to consume an environment variable in your component's template.

  1. From your component class
@Component({
  selector: "app-footer",
})
export class FooterComponent {
  public version = process.env.NG_APP_VERSION;
}
{{ version }}
  1. Using the env pipe
npm install @ngx-env/core
import { NgxEnvModule } from "@ngx-env/core";

@NgModule({
  declarations: [FoooterComponent],
  imports: [CommonModule, NgxEnvModule],
})
export class FooterModule {}
{{ 'process.env.NG_APP_ENV' | env }}
{{ 'NG_APP_ENV' | env }}

Usage in Index.html

You can also access the environment variables starting with NG_APP_ in the index.html.

<!DOCTYPE html>
<html lang="en">
  <head>
    <title>NgApp on %NG_APP_BRANCH_NAME%</title>
  </head>
  <body>
    <app-root></app-root>
  </body>
</html>

Defining Environment Variables

Command Line

Defining environment variables can vary between OSes. It’s also important to know that this manner is temporary for the life of the shell session.

Windows (cmd.exe)

set "NG_APP_NOT_SECRET_CODE=abcdef" && npm start

(Note: Quotes around the variable assignment are required to avoid a trailing whitespace.)

Windows (Powershell)

($env:NG_APP_NOT_SECRET_CODE = "abcdef") -and (npm start)

Linux, macOS (Bash)

NG_APP_NOT_SECRET_CODE=abcdef npm start

In .env

@ngx-env/builder uses dotenv to support loading environment variables from .env files.

.env files are to be stored alongside the package.json.

@ngx-env/builder loads .env files with these specific names for the following NG_APP_ENV values, files on the bottom have less priority than files on the top.

valid .env filenames NG_APP_ENV=* NG_APP_ENV=test
.env ✔️ ✔️
.env.local ✔️ ✖️
.env.${NG_APP_ENV} ✔️ ✔️
.env.${NG_APP_ENV}.local ✔️ ✔️

Expanding .env

You can expand variables already available on your machine for use in your .env

For example:

NG_APP_VERSION=$npm_package_version
NG_APP_HOSTNAME=$HOSTNAME

Or expand variables local to the current .env file:

DOMAIN=www.example.com
NG_APP_FOO=$DOMAIN/foo
NG_APP_BAR=$DOMAIN/bar

How It Works

I wrote an article on InDepth.dev explaining how it works.

Good Practices

Declare your environment variables in the generated .env.d.ts file

declare var process: {
  env: {
    NG_APP_ENV: string;
    NG_APP_BASE_URL: string;
    NG_APP_VERSION: string;
  };
};

Use process.env inside environment.ts files

We recommend to consume environment variables in Angular environment files, for two reasons:

  • To avoid using process.env in your business code.

    If one day you decide that a variable is no longer linked to the environment but rather to an Angular configuration, you would only have to modify the environment files.

  • To be ready for the day when Angular would implement the consumption of environment variables directly in the CLI.

    If the syntax proposed by Angular CLI to access the environment variables turns out to be different, you would only have to modify the environment files.

Example:

environment.ts

export const environment = {
  production: false,
  baseUrl: process.env.NG_APP.BASE_URL,
  version: process.env.NG_APP_VERSION,
};

footer.component.ts

import { environment } from "src/environments/environment";

@Injectable()
export class SomeService {
  version = environment.version;
  baseUrl = environment.baseUrl;
}

Credits

License

MIT © Chihab Otmani

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].