0

So I have an Angular application that I can build and run in production environment, but cannot run in dev mode. Tried to use this command: npm exec ng serve -c dev --port 4200 --proxy-config proxy.conf.json and adding -c dev shows this error in the console An unhandled exception occurred: Configuration 'dev' is not set in the workspace.

I have a environment folder with environment.prod.ts file and enviromnent.ts file That contains :

//environment.ts
export const environment ={production: true};
//environment.prod.ts
export const environment = {development: true};

For the instance, there's a look of my angular.json file :

  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "version": 1,
  "cli": {
    "packageManager": "pnpm",
    "analytics": "f9472df0-6689-4f76-b320-65b56b37836a"
  },
  "newProjectRoot": "projects",
  "projects": {
    "hellooara-web-ui": {
      "projectType": "application",
      "schematics": {
        "@schematics/angular:component": {
          "inlineStyle": true,
          "style": "css"
        }
      },
      "root": "",
      "sourceRoot": "src",
      "prefix": "app",
      "architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "outputPath": "target/classes/static",
            "index": "src/index.html",
            "main": "src/main.ts",
            "polyfills": "src/polyfills.ts",
            "preserveSymlinks": true,
            "tsConfig": "tsconfig.app.json",
            "aot": true,
            "assets": [
              {
                "glob": "*",
                "input": "node_modules/mch-common/assets/images",
                "output": "assets/mch-common/images"
              },
              {
                "glob": "*.json",
                "input": "node_modules/mch-common/assets/i18n",
                "output": "assets/mch-common/i18n"
              },
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "vendorChunk": true,
            "extractLicenses": false,
            "buildOptimizer": false,
            "sourceMap": true,
            "optimization": false,
            "namedChunks": true
          },
          "configurations": {
            "production": {
              "fileReplacements": [
                {
                  "replace": "src/environments/environment.ts",
                  "with": "src/environments/environment.dev.ts"
                }
              ],
              "optimization": true,
              "outputHashing": "all",
              "sourceMap": false,
              "namedChunks": false,
              "extractLicenses": true,
              "vendorChunk": false,
              "buildOptimizer": true,
              "budgets": [
                {
                  "type": "initial",
                  "maximumWarning": "2mb",
                  "maximumError": "5mb"
                },
                {
                  "type": "anyComponentStyle",
                  "maximumWarning": "6kb",
                  "maximumError": "100kb"
                }
              ]
            }
          },
          "defaultConfiguration": "development"
        },
        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "hellooara-web-ui:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "hellooara-web-ui:build:production"
            }
          }
        },
        "extract-i18n": {
          "builder": "@angular-devkit/build-angular:extract-i18n",
          "options": {
            "browserTarget": "hellooara-web-ui:build"
          }
        },
        "test": {
          "builder": "@angular-devkit/build-angular:karma",
          "options": {
            "main": "src/test.ts",
            "polyfills": "src/polyfills.ts",
            "tsConfig": "tsconfig.spec.json",
            "karmaConfig": "karma.conf.js",
            "assets": [
              "src/assets"
            ],
            "styles": [
              "src/styles.scss"
            ],
            "scripts": []
          }
        },
        "lint": {
          "builder": "@angular-eslint/builder:lint",
          "options": {
            "lintFilePatterns": [
              "src/**/*.ts",
              "src/**/*.html"
            ]
          }
        },
        "e2e": {
          "builder": "@angular-devkit/build-angular:protractor",
          "options": {
            "protractorConfig": "e2e/protractor.conf.js",
            "devServerTarget": "hellooara-web-ui:serve"
          },
          "configurations": {
            "production": {
              "devServerTarget": "hellooara-web-ui:serve:production"
            }
          }
        }
      }
    }
  }
}

I already tried to add an environment.dev file in environment folder, add environment into angular.json file.

Nathan T.
  • 277
  • 1
  • 8
Alif Dev
  • 15
  • 5

2 Answers2

0

Your error is correct.

Examine the following part of your angular.json:

        "serve": {
          "builder": "@angular-devkit/build-angular:dev-server",
          "options": {
            "browserTarget": "hellooara-web-ui:build"
          },
          "configurations": {
            "production": {
              "browserTarget": "hellooara-web-ui:build:production"
            }
          }
        },

When you run ng serve -c dev what the angular cli does is use the "@angular-devkit/build-angular:dev-server" script and it looks for the configuration "dev" to override the default options (in "serve" > "options"). But you don't have a "dev" configuration, only a "production" one.

You can therefore run

ng serve (this is development by default)
ng serve -c production

Advanced: once you get comfortable you can add new configurations as you like (testing/staging/etc) with custom options. The available options are usually defined in a schema co-located where the "@angular-devkit/build-angular:dev-server" script is in node_modules.


Update per comment

The common form for environments is to do the following, referring in your application to environment.ts. You then set fileReplacements to replace environment.ts with environment.prod.ts. See https://angular.io/guide/build#configure-target-specific-file-replacements

//environment.ts
export const environment = { production: false };
//environment.prod.ts
export const environment = { production: true };
Andrew Allen
  • 6,512
  • 5
  • 30
  • 73
  • thats what I meant in my answer, isn't it? – Vigneshwaran Sourirajalu Feb 13 '23 at 12:10
  • @VigneshwaranSourirajalu you've lost me? – Andrew Allen Feb 13 '23 at 12:28
  • Thanks for the answer, but I encounter an other error now... `Error: src/main.ts:6:17 - error TS2339: Property 'production' does not exist on type '{ development: boolean; }'. ` In main.ts : main.ts : if (environment.production) { enableProdMode(); } Looks like i need to enable something there to allow dev config to build – Alif Dev Feb 13 '23 at 13:07
  • @AlifDev see my updated post. The full link gives a full overview of things you may want to do for configuring for building and serving your app – Andrew Allen Feb 13 '23 at 13:20
-1

There is something called "serve" property in angular.json

"serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "configurations": {
        "devServer": {
          "browserTarget": "myApp:build:devServer"
        },
}

once did that try running

 ng serve -c devServer

UPDATE 1:

 "serve": {
      "builder": "@angular-devkit/build-angular:dev-server",
      "options": {
        "browserTarget": "hellooara-web-ui:build"
      },
      "configurations": {
        "production": {
          "browserTarget": "hellooara-web-ui:build:production"
        },
        "development": {
          "browserTarget": "hellooara-web-ui:build:development"
        }
      }
    },
  • tried, and stil have an error, but I am pretty sure it's something about build, because I have that for the build on prod : ` "build-prod": "pnpm exec ng build --base-href=/smniotomt/ --configuration=development", ` And for dev I have that : `` "build-prod": "pnpm exec ng build --base-href=/smniotomt/ ", Also I have that in main.ts : if (environment.production) { enableProdMode(); } – Alif Dev Feb 09 '23 at 11:01
  • Is that the same error or a different error? – Vigneshwaran Sourirajalu Feb 09 '23 at 11:06
  • Yes the same : An unhandled exception occurred: Configuration 'devServer' is not set in the workspace. – Alif Dev Feb 09 '23 at 11:48
  • Did you set your devServer in the configurations array? Under projects -> hellooara-web-ui -> architect -> build -> configurations – Vigneshwaran Sourirajalu Feb 09 '23 at 11:55
  • Because i can only able to see production configuration alone. thats why! – Vigneshwaran Sourirajalu Feb 09 '23 at 11:56
  • I've set devServer inside the angular.json file under serve -> configuration – Alif Dev Feb 09 '23 at 13:37
  • Oh I see what you mean. THat's what I have there : "configurations": { "production": { "fileReplacements": [ { "replace": "src/environments/environment.ts", "with": "src/environments/environment.prod.ts" } ], ... } }, – Alif Dev Feb 09 '23 at 13:59
  • Howerver, i added in configuration tat : ` "devServer":{ "fileReplacements": [ {"replace": "src/environments/environment.ts", "with": "src/environments/environment.dev.ts"} ] }` Not sure if it's enough but I get an error from main.ts : `Error: src/main.ts:4:29 - error TS2306: File '/home/msouhail/IdeaProjects/object-management-tool/objectmanagementtool-web-ui/src/environments/environment.dev.ts' is not a module. 4 import { environment } from './environments/environment'; ` – Alif Dev Feb 10 '23 at 08:47
  • Any clue ? still stuck :/ – Alif Dev Feb 13 '23 at 07:56
  • Thanks, updated my configuration but still have same error. Inside architect->build->configuratoin, did I have to setup also development with filereplacement, like it's done with production ? – Alif Dev Feb 13 '23 at 10:35