Aniruddha Das
Aniruddha Das

Reputation: 21688

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

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

Upvotes: 48

Views: 34968

Answers (11)

Amit Singh
Amit Singh

Reputation: 299

As per the latest (Version 12+) guidelines by Angular is that don't use full compilation mode for publishing your library to npm.

The Solution is to set your configuration Accordingly in tsconfig.lib.json or tsconfig.lib.prod.json

If using an Angular version less than 12 you can go with ivy false.

"angularCompilerOptions": {
   "enableIvy": false
 }

From Angular version 12 Ivy will consider true by default. We can't control it directly as Angular will complain.

So the correct suggested option to publish your library is:

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

Upvotes: 0

umunBeing
umunBeing

Reputation: 661

Solved this by simply adding the following to tsconfig.lib.prod.json

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

Upvotes: 4

Sahil Babbar
Sahil Babbar

Reputation: 599

Angular team doesn't allow to publish library in full Ivy mode to NPM as it is highly probable that applications that will consume it, will be built using partial Ivy mode. This will cause compatibility problems.

So, to get out of this situation, you may use the following hack:

Assumption: You will ensure that your consumer application & library are built using full Ivy mode. And any application built using partial Ivy mode will not consume your library. Moreover, both application and library are built using exact same version of Angular.

Hack: Just before publishing your library, you can remove the script from dist/.../package.json, that prevents the library from publishing.

More information: This has been discussed at length with Angular team here. To summarize you may start reading the thread from here.

Upvotes: -1

valdefreaks
valdefreaks

Reputation: 81

In your angular.json file, you have a build object in your library tree. Inside build, you have a configurations object, that contains a production object and probably a development object too.

Inside build object define a new property called defaultConfiguration, and set the value: production that match with the name of the production property inside configurations object.

Your angular.json file should look like this:

"architect": {
  build": {
    "builder": "@angular-devkit/build-ng-packagr:build",
    "options": {
      "tsConfig": "projects/ngx-custom-tooltip/tsconfig.lib.json",
      "project": "projects/ngx-custom-tooltip/ng-package.json"
    },
    "configurations": {
      "production": {
        "tsConfig": "projects/ngx-custom-tooltip/tsconfig.lib.prod.json"
      },
      "development": {
        "tsConfig": "projects/ngx-custom-tooltip/tsconfig.lib.json"
      }
    },
    "defaultConfiguration": "production"
  },
  ...
}

Your tsconfig.lib.prod.json should contains this object:

"angularCompilerOptions": {
  "enableIvy": false
}

Finally, yo can execute ng build your-library-name

Upvotes: 2

ASANIAN
ASANIAN

Reputation: 400

Adding

"compilationMode": "partial"

to

"angularCompilerOptions": {

}

Solved my problem

Upvotes: 1

Francesco Borzi
Francesco Borzi

Reputation: 61834

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

Upvotes: 24

Aniruddha Das
Aniruddha Das

Reputation: 21688

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

Upvotes: 74

bersling
bersling

Reputation: 19262

In addition to adding the --prod flag, if you're running on a really old version, you'll also need the following update to angular.json:

    "myproject": {
      ...
      "architect": {
        "build": {
          ...
          "configurations": {
            "production": {
              "tsConfig": "projects/mypath/tsconfig.lib.prod.json"
            }
          }
        },

with the content of that file being:

{
  "extends": "./tsconfig.lib.json",
  "angularCompilerOptions": {
    "enableIvy": false
  }
}

You can check if you're on such an old version if you still have the following lines in angular.json:

            "production": {
              "project": "projects/tsmean/spinner/ng-package.prod.json"
            }

Upvotes: 3

menrodriguez
menrodriguez

Reputation: 285

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

Upvotes: 6

Melchia
Melchia

Reputation: 24244

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

Upvotes: 8

codeepic
codeepic

Reputation: 4102

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.

Upvotes: 23

Related Questions