Skip to content

Quick Start

Pick the package that matches how you build your app. They all read the same .env files and share the same options, so you can mix and match.

The most popular way to use .env files in Angular. It supports the modern application/esbuild builder as well as Webpack, and requires no configuration.

1. Add it to your Angular project

Terminal window
ng add @ngx-env/builder

2. Define NG_APP_* variables in a .env file

.env
NG_APP_API_URL=https://api.example.com
NG_APP_VERSION=$npm_package_version

Only variables prefixed with NG_APP_ are exposed to your app.

3. Use them in your code

@Component({
selector: 'app-footer',
template: `{{ version }} — {{ apiUrl }}`,
})
export class FooterComponent {
apiUrl = import.meta.env.NG_APP_API_URL; // recommended
version = process.env.NG_APP_VERSION; // also supported
}

…or directly in index.html:

<title>My App %NG_APP_VERSION%</title>

4. Run Angular as usual

Terminal window
npm start

Using a bundler instead?

dotenv-run ships plugins that share the exact same behaviour as the CLI — .env.* files, variable expansion and monorepo cascading all work the same way.