如何在yarn中指定本地registry?

问题描述 投票:0回答:7

我有一个本地nexus服务器,我希望yarn在上线之前先浏览一下它,基本上

prefer-offline

yarn install \
  --prefer-offline \
  --cache-folder C:\folder\yarn-cache \
  --preferred-cache-folder C:\folder\yarn-cache \
  --non-interactive \
  --no-lockfile \
  --registry http://server/repository/npm-group

Yarn 无法离线找到依赖项,然后无法上线。不知道能不能解决。

node.js nexus yarnpkg
7个回答
50
投票
  • 创建一个 npm(托管) 存储库以用作您的私有注册表。 我相信你已经做到了。
  • 创建一个 npm(代理) 存储库指向您选择的外部存储库 (https://registry.yarnpkg.comhttps://registry.npmjs.org/)。
  • 创建一个 npm(组),将您的私有注册表放在第一个位置,将代理注册表放在第二个位置。
  • yarn 指向您的组存储库:
    yarn config set registry http://nexus.local/repository/npm-group/
    。如果您的 Nexus 配置需要,请验证纱线:
    yarn login

还要警惕 yarn 使用您可能拥有的任何 npm 配置:https://github.com/yarnpkg/yarn/issues/4862

Yarn v2+ 更新:

Yarn v2+ 使用不同的配置密钥来更新 NPM 注册表,称为 npmRegistryServer(有关详细信息,请参阅其迁移页面):

yarn config set npmRegistryServer http://nexus.local/repository/npm-group/

36
投票

您可以使用以下命令在yarn中设置不同的注册表:

yarn config set registry <url-to-your-registry>

输入以下命令验证是否已设置:

yarn config get registry

对于注册表中的一次性更改,您可以使用变量

YARN_REGISTRY
,如下所示:

YARN_REGISTRY="<url-to-your-registry>" yarn config get registry

YARN_REGISTRY="<url-to-your-registry>" yarn publish

8
投票

您可以使用

.yarnrc
文件并添加
registry "<your repo URL>"
请参阅 - yarnrc


3
投票

注意!

如果您在

publishConfig.registry
中定义您的
package.json

  "publishConfig": {
    "registry": "https://registry.npmjs.org"
  },

它将仍然覆盖注册表,即使

--registry
参数被赋予
yarn publish


3
投票

另外不要忘记在更改注册表后重新生成yarn.lock 文件。这是必要的,因为yarn.lock包含旧注册表的链接,并将尝试从该链接安装依赖项。


2
投票

嘿,Yarn 1 位用户

  1. 如果您想为特定存储库配置注册表,它应该转到存储库中的

    .npmrc
    文件。
    .yarnrc
    应该 配置额外的 Yarn 功能

  2. 但是,如果您将其放入.yarnrc文件中,则

    OK
    。 Yarn 将读取
    .npmrc
    .yarnrc
    文件。

  3. 如果您想全局配置注册表,请运行以下命令(例如:我正在使用

    https://registry.npmmirror.com
    注册表):

    yarn config set registry https://registry.npmmirror.com
    

    请注意,将此注册表添加到

    .yarnrc
    您的用户主目录 (
    ~/.yarnrc
    )。添加的行是这样的:

    registry "https://registry.npmmirror.com"
    

对于 Yarn 2 用户,尝试这个答案

对于 NPM 用户

  1. 为存储库配置注册表:

    npm config --location=project set registry https://registry.npmmirror.com
    
  2. 全局配置注册表(

    ~/.npmrc
    ):

    npm config --location=global set registry https://registry.npmmirror.com
    

1
投票

yarn 3 a.k.a. Berry
的情况:

yarn config set 'npmRegistries["http://npm.example.com"]'
© www.soinside.com 2019 - 2024. All rights reserved.