Configuration 'production' could not be found in project 'my-lib'
Asked Answered
C

4

22

I am building a library with Angular 6.1.0

  • ng new lib-demo
  • ng generate library my-lib

All the articles advise running the build for the library with a --prod flag like so:

ng build my-lib --prod

However, this throws an error

Configuration 'production' could not be found in project 'my-lib'.

Which is probably correct because when I look at the angular.json there is no definition for a production build configuration in the library project. It is present for the application project only.

the following is what I have under the build configuration for library project that uses ng-packagr

"build": {
      "builder": "@angular-devkit/build-ng-packagr:build",
      "options": {
        "tsConfig": "projects/my-lib/tsconfig.lib.json",
        "project": "projects/my-lib/ng-package.json"
      }
    }

So the question here is that is the --prod flag not required anymore and just running ng build m-lib will generate a prod build?

Looking at the contents of dist folder it looks so but I am not 100% sure. If someone could validate this, it will be great.

Cathee answered 13/9, 2018 at 18:13 Comment(0)
M
24

Beginning with version 6.1, Angular always does a production build of our library, i.e. in new versions of Angular we don't need the --prod flag anymore when building it, libraries are always built in AOT mode. To ensure, you can take a look at these issues in Angular-CLI repository:

https://github.com/angular/angular-cli/issues/12290

https://github.com/angular/angular-cli/issues/12226

And this article ("Building the Library" section):

https://blog.angularindepth.com/creating-a-library-in-angular-6-87799552e7e5

If you are still using version 6.0.x (or lower) you will want to use the --prod flag when building your library.

You still have an option to pass a configuration as a parameter if needed: ng build --configuration=configuration (see docs). If necessary, you can specify build rules in angular.json, for example, for production build:

"configurations": {
    "production": {
        // Options here
    }
}

And the command should be ng build --configuration=production.

Mcandrew answered 26/11, 2018 at 23:11 Comment(1)
Thanks for validating it. I suspected that it did but wanted to be sureCathee
S
19

In Angular 6+ it is ng build --configuration=production

Then put a production configuration in angular.json

 "configurations": {
        "production": {
          "fileReplacements": [
            {
              "replace": "src/environments/environment.ts",
              "with": "src/environments/environment.prod.ts"
            }
          ],
          "optimization": true,
          "outputHashing": "all",
          "sourceMap": false,
          "extractCss": true,
          "namedChunks": false,
          "aot": true,
          "extractLicenses": true,
          "vendorChunk": false,
          "buildOptimizer": true
        }
      }
Suffruticose answered 13/9, 2018 at 18:35 Comment(1)
Having same issue, this solution doesn't solve the problemCondemnatory
E
0

under the architect section look for "configurations", if you don't have any. try adding one like below.

"configurations": {
      "production": {
      "project": "projects/PROJECT-NAME/ng-package.json"
    }
}

your architect section should like this



"architect": {
    "build": {
      "builder": "@angular-devkit/build-ng-packagr:build",
      "options": {
        "tsConfig": "projects/PROJECT-NAME/tsconfig.lib.json",
        "project": "projects/PROJECT-NAME/ng-package.json"
      },
      "configurations": {
        "production": {
          "project": "projects/PROJECT-NAME/ng-package.json"
        }
      }
    }
Emmeram answered 13/6, 2019 at 20:27 Comment(0)
L
0

the --prod is no longer needed as the library is always build in AOT mode and without the --prod You need to run only ng build

Levenson answered 28/3, 2020 at 16:16 Comment(1)
Thanks Rahul. Is there any supporting Angular document which clarifies more aspectsJersey

© 2022 - 2024 — McMap. All rights reserved.