简体   繁体   English

npm 当 package 具有公共依赖项时,私有 package 安装失败

[英]npm install of private package fails when package has public dependencies

Lets say I have a private npm repository, hosted within JFrog artifactory : https://my-domain.com/artifactory/api/npm/my-repo .假设我有一个私有的 npm 存储库,托管在JFrog artifactory中: https://my-domain.com/artifactory/api/npm/my-repo In this repository I published one npm package: my-package , which builds fine.在这个存储库中,我发布了一个 npm package: my-package ,构建良好。 my-package has a dependency (or more) to public npm packages eg lodash . my-package依赖于(或更多)公共 npm,例如lodash

However, when I create a new project and attempt to install my-package I get the following error:但是,当我创建一个新项目并尝试安装my-package时,出现以下错误:

$ npm install my-package --registry https://my-domain.com/artifactory/api/npm/my-repo
npm ERR! code E404
npm ERR! 404 Not Found - GET https://my-domain.com/artifactory/api/npm/my-repo/lodash - not_found
npm ERR! 404
npm ERR! 404  'lodash^4.17.11' is not in the npm registry.
npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
npm ERR! 404 It was specified as a dependency of 'my-package'
npm ERR! 404
npm ERR! 404 Note that you can also install from a
npm ERR! 404 tarball, folder, http url, or git url.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\<username>\AppData\Roaming\npm-cache\_logs\2019-04-29T12_47_51_647Z-debug.log

It appears as though npm is searching within my private repository for all the dependencies my-package requires when I specify the --registry option when running an npm install .当我在运行npm install时指定--registry选项时,似乎npm正在我的私有存储库中搜索my-package所需的所有依赖项。 However, my-package is dependent upon public dependencies, which are not in my private registry.但是, my-package依赖于公共依赖项,这些依赖项不在我的私有注册表中。

My Question我的问题

How to install an npm package from a private registry that has public dependencies?如何从具有公共依赖项的私有注册表安装 npm package? Perhaps this is also just a JFrog issue?也许这也只是 JFrog 的问题?

Any help would be greatly appreciated!任何帮助将不胜感激!

By specifying a registry with: --registry https://my-domain.com/artifactory/api/npm/my-repo npm is attempting to resolve all necessary packages, by name and version, from your private repository location : domain.com/artifactory/api/npm/my-repo .通过使用以下命令指定注册表: --registry https://my-domain.com/artifactory/api/npm/my-repo npm 正在尝试从您的私有存储库位置解析所有必要的包,按名称和版本domain.com/artifactory/api/npm/my-repo

To resolve these public dependencies which your private library depends upon you have two options:要解决您的私有库所依赖的这些公共依赖项,您有两种选择:

  1. Set up a Virtual Npm Registry.设置一个虚拟 Npm 注册表。 (Recommend this approach) (推荐这种方法)
  2. Package all necessary dependencies within your private repository.在您的私有存储库中打包所有必要的依赖项。

A Virtual Repository defined in Artifactory aggregates packages from both local and remote repositories. Artifactory 中定义的虚拟存储库聚合来自本地和远程存储库的包。 This allows you to access both locally hosted npm packages and remote proxied npm registries from a single URL defined for the virtual repository.这允许您从为虚拟存储库定义的单个 URL 访问本地托管的 npm 包和远程代理的 npm 注册表

By setting up a virtual repository which references both your private repository location and the default public npmjs location you will be able to download your private libraries as well as any public npm package by specifying your above mentioned registry.通过设置一个引用您的私有存储库位置和默认公共 npmjs 位置的虚拟存储库,您将能够通过指定上述注册表来下载您的私有库以及任何公共 npm 包。

Since you mentioned JFrog take a look at their confluence page which walks you through the process of creating a virtual repository.既然您提到了JFrog ,请查看他们的 confluence页面,该页面将引导您完成创建虚拟存储库的过程。

However, if you decide to use option 2 you will have to package all the necessary dependencies within your private repository.但是,如果您决定使用选项 2,则必须将所有必要的依赖项打包到您的私有存储库中。 Then your private library will be able to properly pull the dependencies it's dependent upon.然后您的私有库将能够正确提取它所依赖的依赖项。 I would advise against this approach since you will be duplicating work already provided by npmjs and you will additionally have to keep updating your private repository to include new libraries or newer versions of existing libraries.我建议不要使用这种方法,因为您将复制npmjs已经提供的工作,并且您还必须不断更新您的私有存储库以包含新库或现有库的更新版本。

Hopefully that helps!希望这会有所帮助!

My solution...我的解决方案...

involved updating my .npmrc file:涉及更新我的.npmrc文件:

I changed registry=https://npm.pkg.my-domain.com我更改了registry=https://npm.pkg.my-domain.com

to @my-private-scope:registry=https://npm.pkg.my-domain.com@my-private-scope:registry=https://npm.pkg.my-domain.com

Here's why这就是为什么

In it, I had specified that registry=https://npm.pkg.my-domain.com (followed by my auth token) so that I could import a private package from the @my-private-scope scope. However since i left at the @my-private-scope: part, I was changing the URL for all decencies, not just the ones that are part of my private organization.在其中,我指定了registry=https://npm.pkg.my-domain.com (后跟我的身份验证令牌),这样我就可以从 @my-private-scope scope 导入私有 package。但是因为我留在@my-private-scope:部分,我正在为所有礼仪更改 URL,而不仅仅是我的私人组织的一部分。 I made the change to specify the my-domain URL for only the dependencies at that scope.我进行了更改以仅针对该 scope 的依赖项指定我的域 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM