51

我已将 Angular 库升级到 Angular 9。但是,当我尝试在另一个 Angular 9 项目中使用该库时,出现如下错误:

目标入口点“mycomponents/entity-selector”缺少依赖项:

 - mycomponents/shared-services
 - mycomponents/spinner
 - mycomponents/text-input

包.json

{
  "$schema": "../../../node_modules/ng-packagr/package.schema.json",
  "name": "entity-selector",
  "version": "0.0.0",
  "ngPackage": {
    "lib": {
      "entryFile": "public_api.ts"
    },
    "dest": "../../../dist/mycomponents/entity-selector"
  }
}

这是一个次要终点,它使用也是次要终点的其他组件。

在库项目中,我是否需要在 ng-packgr 或其他地方定义依赖项?实体选择器组件的模块为其他组件导入适当的模块。这个问题从 Angular 9 开始出现。

预先感谢。

4

9 回答 9

25
ERROR in The target entry-point "primeng" has missing dependencies: - chart.js

ERROR in The target entry-point "primeng" has missing dependencies: - quill

ERROR in The target entry-point "primeng" has missing dependencies: - @fullcalendar/core


npm install --save chart.js
npm install --save quill
npm install --save @fullcalendar/core
于 2020-03-28T17:31:22.947 回答
18

第一个问题

您收到该错误是因为您的测试项目没有在其node_modules/目录中安装这些依赖项。但我相信按照@Renato 的建议执行并强制库的用户手动安装这些依赖项是错误的方法。

为了自动安装缺少的依赖项,有必要在两个地方(在库本身内)添加库的第 3 方依赖项:

  • package.json在库的根目录。我相信你已经做到了。将所有包放在这里可确保node_modules/在运行项目进行开发时根目录下只有一个目录。
  • projects/entity-selector/package.json是 Angular 在构建库时生成的 package.json 文件的基础文件。有必要在此处添加依赖项,以便库的使用者知道他们(嗯,他们的包管理器)需要下载哪些包。我相信这是你目前所缺少的。

第二个问题

在将我的依赖项正确添加到这两个地方后,我得到了构建错误,告诉我应该为我的库使用“peerDependencies”而不是“dependencies”。

这不适用于我的用例,因此为了解决这个问题,我必须明确地将我的依赖项列入白名单。这样做看起来会有所不同,具体取决于您使用的是以下哪一项:

  • projects/entity-selector/package.json
  • projects/entity-selector/ng-package.json.


其中projects/entity-selector/package.json应该是:

{
  "$schema": "../../../node_modules/ng-packagr/package.schema.json",
  "ngPackage": {
    "lib": {
      "entryFile": "public_api.ts"
    },
    "dest": "../../../dist/mycomponents/entity-selector",
    "whitelistedNonPeerDependencies": [
      "mycomponents/shared-services",
      "mycomponents/spinner",
      "mycomponents/text-input"
    ]
  }
}

其中projects/entity-selector/ng-package.json应该是:

{
  "$schema": "./node_modules/ng-packagr/package.schema.json",
  "lib": {
    "entryFile": "public_api.ts"
  },
  "whitelistedNonPeerDependencies": [
    "mycomponents/shared-services",
    "mycomponents/spinner",
    "mycomponents/text-input"
  ]
}

最后,不要忘记构建你的项目,否则当你尝试发布到 NPM 时,ng build --prod你会收到关于新 Ivy 编译器的错误!

于 2020-05-05T20:09:56.577 回答
7

修改您的组件项目以将绝对路径更改为相对路径。

如:

import {xxx} from 'src/xxx/xxx.module';

至:

import {xxx} from '../../xxx/xxx.module';
于 2020-04-21T08:28:07.483 回答
5

我遇到过同样的问题。这是在添加符号链接之后出现的,所以我不得不使用

npm unlink *@your/package*

或者只是在全球范围内

npm unlink
于 2020-05-06T07:03:46.053 回答
2

ngx-quill需要quill已经安装库,所以只需添加它就可以解决我的问题

npm i quill@^1.3.7

于 2021-10-15T00:02:51.273 回答
2

我想我刚刚解决了你描述的情况。我有一个名为的 NPM 包my-pkg,其中包含几个“库”(使用ng g library lib[1..N]等创建)。碰巧我的libN依赖lib1。当我尝试libN在应用程序中使用时,出现错误:

The target entry-point "my-pkg/libN" has missing dependencies:
 - lib1

这是我最初导入的lib1方式libN

// libN.component.ts
import { Lib1Comp } from 'lib1';

这在我构建时有效my-pkgnode_modules问题是“lib1”不能解析为我的应用程序文件夹中的顶级包。在那里,它应该被称为my-pkg/lib1. 所以,让我们更改导入libN.component.ts

// libN.component.ts
import { Lib1Comp } from 'my-pkg/lib1'; // note lib1 is now prefixed with my-pkg

当然,我们的 node_modules 中我们没有my-pkg安装my-pkg,所以找不到my-pkg/lib1,现在my-pkg不编译了。

但是,如果我们能够“安装”my-pkg到它自己的 node_modules 文件夹中呢?让我们尝试通过 NPM 脚本将其复制到那里:

// package.json
...
"scripts": {
    "copy:lib1": "npx copy .\\dist\\lib1\\**\\* .\\node_modules\\my-pkg\\lib1",
    "build:lib1": "ng build --prod lib1 && npm run copy:lib1",
    // repeat copy/build scripts as needed for libs[2..N]
    "build:pkg": "npm run build:lib1 && npm run build:libN"
},

现在,我们运行npm run build:pkg. 它构建lib1然后将其复制到我们的本地node_modules文件夹,现在可以愉快地从您的库和应用程序中libN的路径导入!my-pkg/lib1

于 2020-09-24T21:26:29.497 回答
1

在导入库的项目中,将以下内容添加到tsconfig.json. 这确保tsc能够解决库中子模块之间的“缺失依赖关系”。

 "compilerOptions": {
    "paths": {
      "mycomponents/*": [
        "./node_modules/mycomponents/*"
      ],

有了这个,就没有必要了whitelistedNonPeerDependencies

于 2020-05-16T01:37:07.850 回答
0

对于 Angular 和 Primeng 9

目标入口点“primeng”中的错误缺少依赖项:

  • 图表.js

我之前的导入:

import {ToastModule} from "primeng";

解决方案:

import {ToastModule} from "primeng/toast";
于 2021-12-31T12:08:08.597 回答
0

The target entry-point has missing dependencies使用 启动 Angular 项目时出现错误npm run start。一个对我有用的简单解决方案是将我的项目从 GIT 克隆到我本地机器上的一个新文件夹中,然后npm installnpm run start一个干净的项目上运行。问题消失了。

于 2021-02-26T14:11:13.130 回答