angular 9 library publish error "Trying to publish a package that has been compiled by Ivy"

18,066

Solution 1

UPDATE ANGULAR 12

using the --configuration production option while building the library fixed my issue

ng build --configuration production

Original answer:

using --prod option while building the library fixed my issue

ng build yourLibraryName --prod

Solution 2

According to official Angular docs https://angular.io/guide/ivy#opting-out-of-ivy-in-version-9

it is better to opt out from Ivy compiler to use the older View Engine when building and publishing your library by adding this line:

enableIvy: false

to angularCompilerOptions in tsconfig.json file at the root of your library as can be seen below;

enter image description here

I've tried it after updating my Angular library to Angular version 9 and this one small change worked like a charm.

Solution 3

Fix for Angular 12+ missing component style issue

On Angular 12, while using:

ng build --configuration production

indeed solved the original issue for me, it also introduced a new one: the styles of my library component where completely missing.

I've solved this other problem by replacing:

  "angularCompilerOptions": {
    "enableIvy": false
  }

with:

  "angularCompilerOptions": {
    "compilationMode": "partial"
  }

in projects/my-library/tsconfig.lib.prod.json

Source: https://angular.io/guide/creating-libraries#building-libraries-with-ivy

Solution 4

In my case none of the solutions above worked. I noticed however that in the package.json of my library in the dist folder there is an newly added script (probably added because of this):


  "scripts": {
    "prepublishOnly": "node --eval \"console.error('ERROR: Trying to publish a package that has been compiled by NGCC. This is not allowed.\\nPlease delete and rebuild the package, without compiling with NGCC, before attempting to publish.\\nNote that NGCC may have been run by importing this package into another project that is being built with Ivy enabled.\\n')\" && exit 1"
  }

SO here either remove/replace the prepublishOnly script or publish using npm publish --ignore-scripts

Solution 5

While building my libraries with ng build --prod and enableIvy = false in tsconfig.lib.json I was getting the same "prepublishOnly" script added to my workspace package.json as Melchia had.

The reason why seems to be related to using a private repository via publishConfig/registry in each library's package.json, as stated in https://github.com/angular/angular/issues/37973#issuecomment-656136865

When building Angular libraries for publishing, use ng build --prod, enableIvy = false in library's tsconfig.json and, if working with a private repository, npm publish --ignore-scripts

Share:
18,066
Aniruddha Das
Author by

Aniruddha Das

A self-motivated full-stack, multi-domain and solutions-orientated JavaScript, ES6, Typescript, Angular (7), AngularJS, jQuery, HTML, CSS, JAVA, J2EE, Spring, PHP, Shell Script, PL SQL, MySQL, AWS, Google App Engine, Google Datastore, JPA, Cucumber BDD Professional with more then 11+ Years of IT experience in a various IT stages including Design, developing, Unit testing and enhancement and support application.

Updated on June 05, 2022

Comments

  • Aniruddha Das
    Aniruddha Das almost 2 years

    I migrated my angular 8.x.x project to angular 9.x.x and when I try to publish my library, it fails with below error

    npm ERR! @candiman/[email protected] prepublishOnly: node --eval "console.error('ERROR: Trying to publish a package that has been compiled by Ivy. This is not allowed.\nPlease delete and rebuild the package, without compiling with Ivy, before attempting to publish.\n')" && exit 1

    is there anything changed in the angular 9

  • codeepic
    codeepic about 4 years
    And what is your --prod configuration?
  • Aniruddha Das
    Aniruddha Das about 4 years
    actually --prod is a flag passed to angular cli configuration to say its a prod build so dont do a aot/ivy build
  • codeepic
    codeepic about 4 years
    Yes - but it still has to be set up as production configuration for your library in angular.json file - otherwise you will get an error saying there is no production configuration when you try to run in.
  • Aniruddha Das
    Aniruddha Das about 4 years
    @codeepic, hann, good point. I actuality did not tried that. thanks for adding that as a note
  • Johansrk
    Johansrk about 4 years
    adding --prod worked and no, you should not write production. The environment config and the flag --prod are 2 different things
  • rbelow
    rbelow almost 4 years
    @AniruddhaDas also be sure to do ng build yourLibraryName --prod after ng test --watch=false. Ng test will also compile your library getting then the error: ERROR: Trying to publish a package that has been compiled by NGCC. This is not allowed.
  • Ben Winding
    Ben Winding over 3 years
    Wouldn't it be great if this was included in the output message!??!!!!
  • Dmitry Grinko
    Dmitry Grinko over 3 years
    Is it because you're trying to publish a package that starts from 'ng-'?
  • Eric Aya
    Eric Aya almost 3 years
    This has already been mentioned in Francesco's anser.
  • Leepians
    Leepians almost 3 years
    Actually this is the exact solution, no long modification needed as discussed in answer you are referencing