Angular: 如何切换存储库以从公司内部源下载 node_modules 依赖项

Angular: How to switch repository to download node_modules dependencies from internal company source

我有一个内部公司工件(存储库),需要从中安装所有依赖库。当我这样做 npm install 时,我无法从互联网上下载它们。

我查看 package-lock.json 内部,我发现所有依赖项都在那里,它似乎正在使用 registry.npmjs.org,如下例所示。

"ngx-bootstrap": {
  "version": "3.0.1",
  "resolved": "https://registry.npmjs.org/ngx-bootstrap/-/ngx-bootstrap-3.0.1.tgz",
  "integrity": "sha512-ni91yYtn8ldgf/pxrlwl9lkVcLURGzopSpJnEbbgG1v1EZWTobI8y7J3mx4Kxptkn0EeiQwnLel67G7XJSox4A=="
},

那么,我是否必须检查整个 package-lock.json 文件并将 registry.npmjs.org 替换为我公司的内部存储库?例如如下

"ngx-bootstrap": {
  "version": "3.0.1",
  "resolved": "https://[mycompany.internal-artifactory.com]/ngx-bootstrap/-/ngx-bootstrap-3.0.1.tgz",
  "integrity": "sha512-ni91yYtn8ldgf/pxrlwl9lkVcLURGzopSpJnEbbgG1v1EZWTobI8y7J3mx4Kxptkn0EeiQwnLel67G7XJSox4A=="
},

谢谢。

获取贵公司的包管理器 URL,然后执行 set registry 命令。然后它应该在执行 npm install

时从贵公司的来源中提取

修改您的全局 .npmrc 文件。默认的文件位置通常是 C/Users/'Your name'/.npmrc。这是假设您的公司允许您修改它。

然后您可以将注册表位置改回默认值

registry=https://registry.npmjs.org/

如果您想将其更改为您公司的存储库,只需将注册表 URL 更改为您公司的 URL。

您可以在 .npmrc 文件中设置这样的信息。这与通过命令行设置值相同,但它在文件中用于分发和版本控制。

来自official docs

The four relevant files are:

  • per-project config file (/path/to/my/project/.npmrc)
  • per-user config file (~/.npmrc)
  • global config file ($PREFIX/etc/npmrc)
  • npm builtin config file (/path/to/npm/npmrc)

All npm config files are an ini-formatted list of key = value parameters. Environment variables can be replaced using ${VARIABLE_NAME}

在任何这些文件中,根据您希望注册表设置应用的范围,您可以简单地放置以下内容:

registry=https://[mycompany.internal-artifactory.com]

如果您希望此更改在您的计算机上是全局的,请按照文档中的说明修改 ~/.npmrc。我建议修改项目根目录中的 .npmrc 文件,这样您团队中的其他开发人员就不需要进行相同的更改。