npmrc 同一范围的多个注册表

Kou*_*sha 9 node.js npm npm-registry

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

我试着做

@mycompany:registry=https://company.registry.com
@mycompany:registry=https://registry.npmjs.org
Run Code Online (Sandbox Code Playgroud)

但这不起作用。

Jua*_*ado 5

你不能。不幸的是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}
Run Code Online (Sandbox Code Playgroud)

如您所见,没有配置遥控器(上行链路),它完全离线。

然后我将运行我的提案Verdaccio ( http://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}
Run Code Online (Sandbox Code Playgroud)

作为 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
Run Code Online (Sandbox Code Playgroud)

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

拥有代理注册表使您的团队的流程更加透明,一切都是集中的,更高效,ihmo。

我希望这有帮助。