angular-cli: Angular 6 app with angular library: `ng build` works but `ng build --prod` throws "ERROR in ./src/app/app.component.ngfactory.js"

Bug Report or Feature Request (mark with an x)

- [x] bug report -> please search issues before submitting
- [ ] feature request

Area

- [x] devkit
- [ ] schematics

Versions

node v8.10.0 npm 6.1.0 macOS High Sierra “@angular/core”: “^6.0.3”, “@angular/cli”: “~6.0.8”,

Repro steps

I created an angular library ng generate library common-lib --prefix=canyalib

Added these components and services to the library module

Exported them in the public_api.ts

Used the library components successfully in the project app

The CommonLibModule is imported in the app.module.ts

I run ng serve and ng build without errors. But when I run ng serve --prod or ng build --prod. The build fails after the 92% ... UglifyTask :

The log given by the failure

ERROR in ./dist/common-lib/canyaio-common-lib.ngfactory.js
Module not found: Error: Can't resolve '@canyaio/common-lib' in '/Users/GustavoIbarra/Projects/Angular/canya-common/dist/common-lib'
ERROR in ./src/app/app.module.ngfactory.js
Module not found: Error: Can't resolve '@canyaio/common-lib' in '/Users/GustavoIbarra/Projects/Angular/canya-common/src/app'
ERROR in ./src/app/app.component.ngfactory.js
Module not found: Error: Can't resolve '@canyaio/common-lib' in '/Users/GustavoIbarra/Projects/Angular/canya-common/src/app'

Desired functionality

This error won’t let me run the app with library modules in a production environment.

Mention any other details that might be useful

If I import the packaged library in an external Angular 6 application, the same happens: ng serve or ng build lets the app run without problems. ng serve --prod or ng build --prod fails.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 7
  • Comments: 27

Most upvoted comments

Can you check if this has the same effect

npm run ng build -- --prod

i have encountered same issue. Check your aot build of library. Check its metadata.json file, importAs:“libname”, it should be “@libname”. that why ngfactory.js searching for “libname” rather than “@libname”.

so solution is change package.json of your library. ‘name’ property should be “@LIBNAME”.

Thanks @daniel-nagy I encountered this issue and got it resolved by adding another path in tsconfig.json. ngfactory is looking for the name defined in library’s package.json. It could be different from the original name used to create the library using angular cli. matching the path like below resolved the issue (@my-repo/my-lib is the package name defined in library package.json).

"paths": {
      "my-lib": [
        "dist/my-lib"
      ],
      "@my-repo/my-lib": [
        "dist/my-lib"
      ],
      "my-lib/*": [
        "dist/my-lib/*"
      ]
    }

Incase someone else lands here, I had the same issue but my problem was I prefixed the path with an @ in my tsconfig.json file. Development builds worked find but when building for production I got the following error:

ERROR in ./dist/my-library/my-library.ngfactory.js
Module not found: Error: Can't resolve 'my-library' in '/path/to/MyWorkSpace/dist/my-library'
ERROR in ./src/app/app.module.ngfactory.js
Module not found: Error: Can't resolve 'my-library' in '/path/to/MyWorkSpace/src/app'

As you can see, the generated factory classes are looking for my-library, not @my-library. I just removed the @ from my path mappings and it built fine.

To All, i think this issue is replicable even in a sample project created using CLI

Steps | All using Ng CLI Add a proj Add a couple of libraries to the project. Do a Ng Build --prod

WORKS FINE

Now rename the library entries with alias names & have the paths for aliases resolved in the tsConfig. eg. import {} from ‘library1’ to import {} from ‘@my/library

Now, ‘ng build’ will work fine but ‘ng build --prod’ fails.

Happy to know if there is something missing/incorrect in the above steps.

After hours messing about with this I found this article stating that the local library package needs to be installed as well as being aliased in tsconfig.json. This step is missing from the current cli docs. https://medium.com/@SirMaxxx/angular-6-creating-a-shareable-control-library-6a27f0ebe5c2

The author mentions that the local install can cause problems with npm install next time you perform a clean checkout of the repo, To get around this I used npm install file:dist/mylibrary --no-save to create the link without touching package.json.