angular 9 库发布错误“尝试发布已由 Ivy 编译的包”

Ani*_*Das 28 angular-cli angular angular-library angular-cli-v9

我将我的angular 8.x.x项目迁移到angular 9.x.x,当我尝试发布我的库时,它失败并显示以下错误

npm 错误!@candiman/website@9.0.0 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

有什么变化吗 angular 9

Ani*_*Das 50

更新角度 12

--configuration production在构建库时使用该选项修复了我的问题

ng build --configuration production
Run Code Online (Sandbox Code Playgroud)

原答案:

--prod在构建库时使用选项修复了我的问题

ng build yourLibraryName --prod
Run Code Online (Sandbox Code Playgroud)

  • @AniruddhaDas 还请务必在“ng test --watch=false”之后执行“ng build yourLibraryName --prod”。NG test 还将编译您的库,然后出现错误:“错误:尝试发布已由 NGCC 编译的包。这是不允许的。` (2认同)

Fra*_*rzi 24

修复 Angular 12+ 缺少组件样式问题

在 Angular 12 上,使用时:

ng build --configuration production
Run Code Online (Sandbox Code Playgroud)

确实为我解决了最初的问题,它还引入了一个新问题:我的库组件的样式完全丢失。

我通过替换解决了另一个问题:

  "angularCompilerOptions": {
    "enableIvy": false
  }
Run Code Online (Sandbox Code Playgroud)

和:

  "angularCompilerOptions": {
    "compilationMode": "partial"
  }
Run Code Online (Sandbox Code Playgroud)

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

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


cod*_*pic 16

根据官方 Angular 文档https://angular.io/guide/ivy#opting-out-of-ivy-in-version-9

通过添加以下行,在构建和发布您的库时,最好选择退出 Ivy 编译器以使用较旧的视图引擎:

enableIvy: false

到库根目录下 tsconfig.json 文件中的 angularCompilerOptions ,如下所示;

在此处输入图片说明

我在将我的 Angular 库更新到 Angular 版本 9 后尝试过它,这个小小的改变就像一个魅力。


小智 6

在使用ng build --prodenableIvy = falsein构建我的库时tsconfig.lib.json,我将package.jsonMelchia相同的“prepublishOnly”脚本添加到我的工作区中。

publishConfig/registry原因似乎与在每个库中使用私有存储库有关package.json,如https://github.com/angular/angular/issues/37973#issuecomment-656136865中所述

当构建用于发布的 Angular 库时,请在库中ng build --prod使用, 如果使用私有存储库,enableIvy = falsetsconfig.jsonnpm publish --ignore-scripts


Mel*_*hia 5

就我而言,上述解决方案均无效。然而,我注意到在package.json我的库的 dist 文件夹中有一个新添加的脚本(可能是因为这个而添加的):


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

所以在这里要么删除/替换prepublishOnly脚本,要么使用发布npm publish --ignore-scripts