glen-84
glen-84

Reputation: 1998

Is the environment.ts file typically committed or ignored?

Is the file src\environments\environment.ts typically committed, or added to the .gitignore file?

In a new Angular project (8.x) I see that this file is not added to the .gitignore file, and I assume that it's like this because the project imports this file (or a replaced version of it, depending on the specified configuration).

The real question is this: If a developer wishes to make local, developer-specific changes to this file (f.e. a different apiUrl setting), should they:

  1. Keep this file committed with shared defaults, ignore the file locally only when needed (not affecting other developers), and then make local changes.
  2. Initially commit this file with shared defaults, add it to .gitignore for all users, and then make local changes.
  3. Never commit the file (developers would have to create the file when first checking out the code).

Upvotes: 9

Views: 4170

Answers (2)

Adam Falk
Adam Falk

Reputation: 21

Not enough reputation at the moment to comment on Reactgular answer.

With their solution I kept getting the error :

An unhandled exception occurred: Configuration 'localhost' is not set in the workspace.

I was able to get their solution working by adding the following under the serve object:

Underneath production / development in the serve object:

"localhost": {"browserTarget": "recipe-project:build:localhost"}

Upvotes: 2

Reactgular
Reactgular

Reputation: 54801

The real question is this: If a developer wishes to make local, developer-specific changes to this file (f.e. a different apiUrl setting)

The environment.ts file is required by the project, but the other files are not required unless they are part of the current build.

So you can add a new file and name it environment.localhost_default.ts to your project. Inside that file you can put something like this:

/** Copy this file to environment.localhost.ts **/
export const environment = {
    apiUrl: '/** REPLACE WITH LOCALHOST URL **/',
    production: false
};

Now you can add environment.localhost.ts to your .gitignore file.

Inside your angular.json under the project structure. You'll see a section labelled "architect" and you can add new "configurations" for "localhost". This tells Angular that it should use the environment.localhost.ts file instead.

"configurations": {
    "localhost": {
        "fileReplacements": [
            {
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.localhost.ts"
            }
        ],
        ...
    }
}

You can now build or server your project using this environment.

ng serve --configuration=localhost

Since the file is excluded from Git changes will not get added to the repository, but having a default file environment.localhost_default.ts helps other developers get started. If you make a change to that file it will be shared with others.

Upvotes: 10

Related Questions