Search code examples
angularapidevelopment-environment

Angular 8 : set production environment for api base url


Working on angular 8 project and I want to set two environments configuration, one for dev and other for prod :

dev

export const environment = {
  production: false,
  baseUrl: 'http://localhost:8000/api'
};

prod

export const environment = {
  production: true,
  baseUrl: 'https://serverurl/api/api'
};

the problem here is when I run npm run ng build --prod and test after that a register route on my api it doesn't work, on the other side it works when I test with postman !


Solution

  • Use the configuration option to build with the proper environment

    ng build --prod --configuration=production
    

    In your angular.json you should find something like this:

    "architect": {
      "build": {
        "configurations": {
          "production": {
            "fileReplacements": [
              {
                "replace": "src/environments/environment.ts",
                "with": "src/environments/environment.prod.ts"
              }
            ],
    ...