npmrc具有相同范围的多个注册表

时间:2019-02-05 22:23:25

标签: node.js npm npm-registry

是否可以包含同一范围的多个注册表?在我的公司中,我们将@mycompany范围用于公共NPM注册中心和内部注册中心。

我试图做

@mycompany:registry=https://company.registry.com
@mycompany:registry=https://registry.npmjs.org

但这没用。

1 个答案:

答案 0 :(得分:1)

不,您不能。不幸的是npm只能处理一个作用域对等注册表。但是您可以使用一个代理服务器Verdaccio来为您处理分发。 https://verdaccio.org/docs/en/uplinks

我将使用 Verdaccio 来模拟配置示例:

我嘲笑没有外部访问权限(http://localhost:5000/)的“公司注册表”。

storage: /Users/test/.local/share/verdaccio/storage_company_registry

auth:
  htpasswd:
    file: ./htpasswd   
packages:
  '@*/*':
    access: $all
    publish: $authenticated

  '**':
    access: $all
    publish: $all
middlewares:
  audit:
    enabled: true
logs:
  - {type: stdout, format: pretty, level: http}

如您所见,没有配置任何遥控器(上行链路),它完全脱机。

然后,我将运行我的提案Verdacciohttp://localhost:4873/),该提案首先向公司注册机构询问,如果在该处未找到该软件包,它将从公共注册机构(npmjs)获取。

storage: /Users/test/.local/share/verdaccio/storage_proxy

auth:
  htpasswd:
    file: ./htpasswd
uplinks:
  npmjs:
    url: https://registry.npmjs.org/
  company:
    url: https://company.registry.com

packages:
  '@company/*':
    access: $all
    publish: $authenticated
    proxy: company npmjs  
  '**':
    access: $all
    publish: $authenticated
    proxy: npmjs
middlewares:
  audit:
    enabled: true
logs:
  - {type: stdout, format: pretty, level: http}

作为PoC(我通过@babel切换@company),我运行npm install @babel/types --registry http://localhost:4873/。结果是以下

 warn --- config file  - /Users/test/.config/verdaccio/config.yaml
 warn --- Plugin successfully loaded: htpasswd
 warn --- Plugin successfully loaded: audit
 warn --- http address - http://localhost:4873/ - verdaccio/4.0.0-alpha.4
 http --> 404, req: 'GET http://localhost:5000/@babel%2Ftypes' (streaming)
 http --> 404, req: 'GET http://localhost:5000/@babel%2Ftypes', bytes: 0/43
 http --> 200, req: 'GET https://registry.npmjs.org/@babel%2Ftypes' (streaming)
 http --> 200, req: 'GET https://registry.npmjs.org/@babel%2Ftypes', bytes: 0/93375
 http <-- 200, user: null(127.0.0.1), req: 'GET /@babel%2ftypes', bytes: 0/22072
 http --> 200, req: 'GET https://registry.npmjs.org/esutils' (streaming)
 http --> 200, req: 'GET https://registry.npmjs.org/esutils', bytes: 0/23169
 http <-- 200, user: null(127.0.0.1), req: 'GET /esutils', bytes: 0/3854
 http --> 200, req: 'GET https://registry.npmjs.org/to-fast-properties' (streaming)
 http --> 200, req: 'GET https://registry.npmjs.org/to-fast-properties', bytes: 0/8239
 http <-- 200, user: null(127.0.0.1), req: 'GET /to-fast-properties', bytes: 0/1725
 http --> 200, req: 'GET https://registry.npmjs.org/lodash' (streaming)
 http --> 200, req: 'GET https://registry.npmjs.org/lodash', bytes: 0/185410
 http <-- 200, user: null(127.0.0.1), req: 'GET /lodash', bytes: 0/14307
 http <-- 200, user: null(127.0.0.1), req: 'POST /-/npm/v1/security/audits/quick', bytes: 474/146

您的节点程序包管理器将与(http://localhost:4873/进行对话,Verdaccio将尝试从内部注册表中获取程序包,结果为404,在第二次迭代中将从npmjs中获取程序包,结果为200。

拥有代理注册表,对您的团队而言,该过程更加透明,集中且效率更高。

我希望有帮助。