Javascript 使用带有注册表的 myproject/.npmrc

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34652563/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:41:18  来源:igfitidea点击:

using myproject/.npmrc with registry

javascriptnode.jsnpm

提问by antpaw

How do I setup a .npmrc file inside my project where I can define my own private registry? I don't want to have this kind of configuration in my user config .npmrc. Every other developer should be able to just git clonethe project and run npm install.

如何在我的项目中设置一个 .npmrc 文件,我可以在其中定义我自己的私有注册表?我不想在我的用户配置 .npmrc 中有这种配置。每个其他开发人员都应该能够只git clone运行该项目并运行npm install.

This is what I have so far:

这是我到目前为止:

// .npmrc
registry=https://npm.fury.io/AUTH_TOKEN/me/

// package.json:
{
  "name": "webapp",
  "description": "",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "jquery": "1.2.3",
    "myPrivateLibFromNpmFury": "0.0.4"
  }
}

npm install myPrivateLibFromNpmFury

npm install myPrivateLibFromNpmFury

returns

返回

npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/myPrivateLibFromNpmFury

npm ERR! 404 Registry returned 404 for GET on https://registry.npmjs.org/myPrivateLibFromNpmFury

采纳答案by antpaw

As it was pointed out by @Paulpro and @Alexey B. the most parts of it worked already, but I couldn't see it right away, maybe because I didn't reload my bash environment properly. But after that I faced other issue with npm outdatedthat was caused by the registry url. It turns out npm can only have one registry url, (which is pretty crazy) and if you want to use private and public npm-modules you have to proxy the public npm-module registry through your private registry. Luckily fury.io supports that, so in my case instead of using this:

正如@Paulpro 和@Alexey B 所指出的那样,它的大部分内容已经工作,但我无法立即看到它,可能是因为我没有正确重新加载我的 bash 环境。但在那之后,我遇到了npm outdated由注册表 url 引起的其他问题。事实证明,npm 只能有一个注册表 url,(这很疯狂),如果您想使用私有和公共 npm-modules,您必须通过您的私有注册表代理公共 npm-module 注册表。幸运的是 fury.io 支持这一点,所以在我的情况下,而不是使用这个:

//.npmrc
registry=https://npm.fury.io/AUTH_TOKEN/me/

i have to use this:

我必须使用这个:

//.npmrc
registry=https://npm-proxy.fury.io/AUTH_TOKEN/USER_NAME/

UPDATE: It is possible to work around the problem (npm is tied to only one registry). First you have to add a scopeto all of your private packages. Now with .npmrcyou can link the registries for the scopes, and you no longer need any proxies at all.

更新:有可能解决这个问题(npm 只绑定到一个注册表)。首先,您必须为所有私有包添加一个范围。现在,.npmrc您可以链接范围的注册表,并且您根本不再需要任何代理。

//.npmrc
@project_a:registry=https://npm.fury.io/AUTH_TOKEN/USER_NAME/
@project_b:registry=https://npm.fury.io/AUTH_TOKEN/USER_NAME/
@company_a:registry=https://npm.fury.io/AUTH_TOKEN/USER_NAME/

回答by Alexey B.

Noticed to the docs

注意到文档

Per-project config file

When working locally in a project, a .npmrc file in the root of the project (ie, a sibling of node_modules and package.json) will set config values specific to this project.

Note that this only applies to the root of the project that you're running npm in. It has no effect when your module is published. For example, you can't publish a module that forces itself to install globally, or in a different location.

每个项目的配置文件

在项目中本地工作时,项目根目录中的 .npmrc 文件(即 node_modules 和 package.json 的同级)将设置特定于该项目的配置值。

请注意,这仅适用于您在其中运行 npm 的项目的根目录。当您的模块发布时,它不起作用。例如,您不能发布强制自己在全球或不同位置安装的模块。

I tried to create the files you specified in the question(package.json and .npmrc), everything working fine. Maybe you made a typo somewhere?

我尝试创建您在问题中指定的文件(package.json 和 .npmrc),一切正常。也许你在某处打错了字?

frgt$ npm i myPrivateLibFromNpmFury --verbose

npm info using [email protected]
npm info using [email protected]
npm verb request uri https://npm.fury.io/AUTH_TOKEN/me/myPrivateLibFromNpmFury
npm verb request no auth needed
npm info attempt registry request try #1 at 14:36:10
npm verb request id 23f09acc4e7021c7
npm http request GET https://npm.fury.io/AUTH_TOKEN/me/myPrivateLibFromNpmFury
npm http 403 https://npm.fury.io/AUTH_TOKEN/me/myPrivateLibFromNpmFury

回答by Paul

You should use the seamless proxy:

您应该使用无缝代理:

registry=https://npm-proxy.fury.io/AUTH_TOKEN/me/