告诉npm从npm中央安装自定义库依赖项的部分。

huangapple go评论68阅读模式
英文:

How do I tell npm to install custom library dependencies from a custom library from npm central?

问题

I'm using Angular 14. I have this custom library project with this folder structure

  • projects
    • my-library
      • package.json

In package.json, I have

{
"name": "my-library",
"version": "7.0.0",
"peerDependencies": {
"@angular/common": "^14.1.2",
"@angular/core": "^14.1.2"
},
"dependencies": {
"parse-domain": "^3.0.3"
}
}

I have my library stored in a custom repo. However, when I go and try and install this custom library in another project using

$ npm install my-library@latest --registry=https://my-custom-repo.com/storage/npm/artifacts/ --legacy-peer-deps
npm ERR! code E404
npm ERR! 404 Not Found - GET https://my-custom-repo.com/storage/npm/artifacts/parse-domain - not_found
npm ERR! 404
npm ERR! 404 'parse-domain@^3.0.3' is not in this registry.
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\npm-cache_logs\2023-04-10T18_27_45_000Z-debug-0.log

The error above indicates it is attempting to look for the custom library dependency in the custom repo, even though it is just in the normal npm repo. How do I instruct the build command to install other dependencies from npm central rather than my custom repo?

英文:

I'm using Angular 14. I have this custom library project with this folder structure

+ projects
	+ my-library
		- package.json

In package.json, I have

{
  "name": "my-library",
  "version": "7.0.0",
  "peerDependencies": {
    "@angular/common": "^14.1.2",
    "@angular/core": "^14.1.2"
  },
  "dependencies": {
    "parse-domain": "^3.0.3"
  }
}

I have my library stored in a custom repo. However, when I go and try and install this custom library in another project using

$ npm install my-library@latest --registry=https://my-custom-repo.com/storage/npm/artifacts/ --legacy-peer-deps
npm ERR! code E404
npm ERR! 404 Not Found - GET https://my-custom-repo.com/storage/npm/artifacts/parse-domain - not_found
npm ERR! 404
npm ERR! 404  'parse-domain@^3.0.3' is not in this registry.
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\npm-cache\_logs23-04-10T18_27_45_000Z-debug-0.log

The error above indicates it is attempting to look for the custom library dependency in the custom repo, even though it is just in the normal npm repo. How do I instruct the build command to install other dependencies from npm central rather than my custom repo?

答案1

得分: 1

  1. 第一个解决方案

您可以通过为您的库设置作用域来解决问题。
简而言之,您的包应该不是my-library,而是@my-scope/my-library,就像@angular/common一样。
当包有作用域时,您可以在.npmrc中为特定作用域指定特定的注册表,例如:

@my-scope:registry=https://my-custom-repo.com/storage/npm/artifacts/

但对于所有其他包,注册表将保持不变(例如.npmrc中的registry字段)。

有关更多信息,请查看https://docs.npmjs.com/cli/v9/using-npm/scope

  1. 第二个解决方案

如果您可以将所有依赖项发布到您的自定义注册表,您可以忽略使用作用域,将所有包发布到您的注册表(或只是通过它代理到npmjs),然后只使用您的注册表来管理所有包,例如.npmrc

registry=https://my-custom-repo.com/storage/npm/artifacts/
  1. 手动处理

您可以在您的package.json中仅保留公共包(删除my-library),然后运行npm i,它将起作用。
然后,您可以将my-library添加到包中,并在.npmrc中将目标注册表设置为私有:

registry=https://my-custom-repo.com/storage/npm/artifacts/

然后再次运行npm i

英文:
  1. 1st solution

You can solve your problem by scoping your library.
In short, your package should look not my-library, but @my-scope/my-library. Just like @angular/common.
When package is scoped, you can target specific registry for specific scope in .npmrc, for example:

@my-scope:registry=https://my-custom-repo.com/storage/npm/artifacts/

But for all other packages registry will be the same (e.g. .npmrc registry field)

For more info check https://docs.npmjs.com/cli/v9/using-npm/scope

  1. 2nd solution

If you can publish all of your dependecies to your custom registry, you can ignore using scopes, puplish all packages to your registry (or just proxy to npmjs through it) and then just use your registry for all packages, for example .npmrc:

registry=https://my-custom-repo.com/storage/npm/artifacts/
  1. Manual hack

You can leave only public packages in your package json (remove my-library), then run npm i, it will work.
Then u can add my-library to package and target registry to private in .npmrc:

registry=https://my-custom-repo.com/storage/npm/artifacts/

and run npm i again

huangapple
  • 本文由 发表于 2023年4月11日 02:32:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75979715.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定