Gagan Suie
Gagan Suie

Reputation: 330

Angular 6 difference between .env vs environment.ts

I generated a project from https://mean.io. But I am not sure what the difference is between (.env and .env.example) and (environment.ts and environment.prod.ts).

Where should I store all of my config data, secrets, and keys?

Upvotes: 3

Views: 5051

Answers (2)

Chris Linden
Chris Linden

Reputation: 246

environment.ts and environment.prod.ts are used in your angular application for loading different variables depending on where the application is running.

ng serve will run the application with the environemnt.ts file. ng serve --prod will run the application with the environment.prod.ts file.

A good example of this would be for an API url. In development you would use http//:localhost:<port> where as in production the url might be www.<my-api>.com you can use the different environment files to have these switch between builds.

Keep in mind that the angular environment file will be readable to any user on you website. It's a bad idea to keep usernames, passwords or api keys in the environment.ts or environment.prod.ts files.

The .env file is used to store environment variables for the node/express API. This code runs on a server. the require('dotenv').config(); line in the server/config/config.js file is where the file is loaded. Dotenv will populate environment variables that are accessed using process.env.<My_Environment_Variable_Name> using the .env file.

Upvotes: 2

Sajeetharan
Sajeetharan

Reputation: 222682

You should store them in the environment.ts

environment.ts If you refer environment object properties in your Angular project, during development mode i.e. ng serve or ng build all values shall be read from this file.

environment.prod.ts When you build your application for production mode using ng build --prod in that case, all values of environment.ts file shall get overridden by environment.prod.ts files.

The above variables are related to your Angular application. Whereas .env and .env.example are for the Laravel application.

.env as it should be is out of version controlled and ignored when you push your project to any repository. This is for your own safety.

.env.example which contains very generic information is copied as .env on fresh install and a few changes made. ex APP_KE

Upvotes: 10

Related Questions