Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
netpoe opened this issue Jun 27, 2018 · 27 comments

Comments

@netpoe
Copy link

netpoe commented Jun 27, 2018

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.

@netpoe
Copy link
Author

netpoe commented Jun 28, 2018

After adding the encapsulation property to one of the library components, I managed to make it work:

import { Component, OnInit, ViewEncapsulation, Input } from '@angular/core';

@Component({
  selector: 'canyalib-footer',
  templateUrl: './footer.component.html',
  styleUrls: ['./footer.component.css'],
  encapsulation: ViewEncapsulation.Native,
})
export class FooterComponent implements OnInit {

Now the ng build --prod returns no errors.

Please see #11394 . I'm using this library in an external Angular 6 application and even though this build works as expected. It won't work on an external app.

@alan-agius4
Copy link
Collaborator

alan-agius4 commented Jun 29, 2018

I have tried this and I cannot replicate any issue.

Steps I did,

  1. Clone https://github.com/canyaio/Common
  2. npm install
  3. Remove all ViewEncapsulation.Native
  4. ng build common-lib
  5. ng build --prod

Output;

Alans-iMac:Common alan$ ng build common-lib
Building Angular Package
Building entry point '@canyaio/common-lib'
Rendering Stylesheets
Rendering Templates
Compiling TypeScript sources through ngc
Bundling to FESM2015
Bundling to FESM5
Bundling to UMD
Minifying UMD bundle
Copying declaration files
Writing package metadata
Removing scripts section in package.json as it's considered a potential security vulnerability.
Built @canyaio/common-lib
Built Angular Package!
- from: /Users/alan/git/Common/projects/common-lib
- to:   /Users/alan/git/Common/dist/common-lib
Alans-iMac:Common alan$ ng build --prod

Date: 2018-06-29T15:38:58.467Z
Hash: a6b418e2c27ccdc9ac10
Time: 41160ms
chunk {scripts} scripts.a8d156b92ea2ded28ebf.js (scripts) 13.2 kB  [rendered]
chunk {0} runtime.a66f828dca56eeb90e02.js (runtime) 1.05 kB [entry] [rendered]
chunk {1} styles.9e3ca50e8b8e482de370.css (styles) 75 kB [initial] [rendered]
chunk {2} polyfills.2f4a59095805af02bd79.js (polyfills) 59.6 kB [initial] [rendered]
chunk {3} main.20bdf776bfb60813d771.js (main) 635 kB [initial] [rendered]

@alan-agius4
Copy link
Collaborator

Can you check if this has the same effect

npm run ng build -- --prod

@netpoe
Copy link
Author

netpoe commented Jun 29, 2018

@alan-agius4 thanks.

This build is working as expected now.

Would this library work for child modules inside an external Angular 6 app?

I'm having trouble with an Angular 6.0.7 app that uses the packaged library. This app has multiple modules

However, when I use the same library in an app with just the app.module.ts, it works 🤔

Am I missing something in order to use the library inside child modules?
https://github.com/canyaio/Common/tree/master/projects/common-lib/src

@alan-agius4
Copy link
Collaborator

alan-agius4 commented Jun 29, 2018 via email

@netpoe netpoe closed this as completed Jun 29, 2018
@netpoe
Copy link
Author

netpoe commented Jun 29, 2018

Just for future reference, I made it work by rm -rf node_modules and npm install again.

Seems like a cache issue or something

@daniel-nagy
Copy link

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.

@alisha42
Copy link

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".

@tmakin
Copy link

tmakin commented Aug 27, 2018

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.

@affilnost
Copy link

I had the the same problem. I fixed it by using ng-packagr v3.0.3 instead of v2.4.5 when building the npm-package.

@mal90
Copy link

mal90 commented Oct 30, 2018

I fixed this issue by adding the @libname path in the tsconfig.json , under "path" key

@vimalans
Copy link

vimalans commented Nov 17, 2018

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/*"
      ]
    }

@subodhkumar
Copy link

@vimalans thanks... the solution worked for me as well with the similar error...Angular 7

@nickofthyme
Copy link

Also see this post #10643 (comment)

@gmerabishvili
Copy link

Any solution for that issue please?

@hari1928
Copy link

hari1928 commented Feb 18, 2019

These is another way to making build ,
ng build --prod --base-href "PATH OF SERVER" --aot=false --build-optimizer=false

patha like http://demo.com/professionals/admin/

@subodhkumar
Copy link

@hari1928 thats almost the same as dev build. The expectation is it to work on Prod/AOT build

@subodhkumar
Copy link

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.

@alhilali
Copy link

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/*"
      ]
    }

I was able to resolve my issue bu going into *.metadata.json file of that library and making sure "importAs" value is set to the new alias. I'm not sure where is this pulled from? Name tags in package.json is updated correctly.

@andrewantar
Copy link

Still getting this error despite trying every proposed solution

@subodhkumar
Copy link

@andrewantar ... Can you please share the error, from what i experienced...one of the above should fix the issues...
try the build with --verbose option to get the issue in more detail...

@goresc
Copy link

goresc commented Apr 19, 2019

I had the same issue with Angular 7. "ng build" was fine but "ng build --prod" failed on 2 files saying it couldn't find the imported @module, even though that same @module was imported on several other files just fine. It turned out removing unused interfaces pulled from the @module fixed the issue. It seems a mix of used and unused interfaces causes the import to fail (with a bad message about the whole file not being found) -- It didn't complain if all imported items were unused, precompiler probably just removed the entire reference. Also, this was a named @module that just exported Interfaces. Other "normal" unused references from other modules were ignored.

@meysamsahragard
Copy link

I resolve this error by using the followed command, after building the library:
npm install dist/libraryName --save
"dist" is the directory of the library.

@JahyrLopez
Copy link

help I have a problem problem mark me this error when I put the command
ng build --prod

Date: 2019-06-26T19:47:29.429Z
Hash: 47649eb13e262d7eaef3
Time: 45551ms
chunk {0} runtime-es5.741402d1d47331ce975c.js (runtime) 1.41 kB [entry] [rendered]
chunk {1} main-es5.7c04bb8524a34ea08589.js (main) 828 kB [initial] [rendered]
chunk {2} polyfills-es5.b8d6dd293b73bbb8581d.js (polyfills) 132 kB [initial] [rendered]
chunk {scripts} scripts.000eefc49a6f20ab379c.js (scripts) 607 kB [entry] [rendered]

ERROR in scripts.000eefc49a6f20ab379c.js from Terser
Unexpected character '@' [scripts.000eefc49a6f20ab379c.js:17652,1]

@JahyrLopez
Copy link

please help help

@leparfait
Copy link

help I have a problem problem mark me this error when I put the command
ng build --prod

Date: 2019-06-26T19:47:29.429Z
Hash: 47649eb13e262d7eaef3
Time: 45551ms
chunk {0} runtime-es5.741402d1d47331ce975c.js (runtime) 1.41 kB [entry] [rendered]
chunk {1} main-es5.7c04bb8524a34ea08589.js (main) 828 kB [initial] [rendered]
chunk {2} polyfills-es5.b8d6dd293b73bbb8581d.js (polyfills) 132 kB [initial] [rendered]
chunk {scripts} scripts.000eefc49a6f20ab379c.js (scripts) 607 kB [entry] [rendered]

ERROR in scripts.000eefc49a6f20ab379c.js from Terser
Unexpected character '@' [scripts.000eefc49a6f20ab379c.js:17652,1]

I have same error please someone can help us ???

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests