注意: npm 企业版需要 npm 4.4.X 或者更高的版本。你可以在命令行中,运行下面的命令来下载最新的稳定版 npm

  1. [sudo] npm install npm -g

你可以通过下面的方法,配置你的 npm 客户端,使客户端能从你们公司私有的企业版 npm registry上传或者下载:

  • 设置公司 npm 企业版 registry 作为你的默认 registry (推荐)
  • 使用 npmrc 去管理不同 registries 的 配置信息(profile)
  • 配置 作用域(scopes)去指出(point)不同的 registries

设置公司 npm 企业版 registry 作为你的默认 registry

将你的企业版 npm registry 设置为 你的默认 registry —— 这是从你的企业版 npm registry 上传和下载就简单的方式。

在命令行中运行下面的命令,用你公司的 registry 名字来代替命令中的 “your-registry”:

  1. npm config set registry https://registry.your-registry.npme.io/

使用 npmrc 去管理不同 registries 的 配置信息(profile)

如果你需要向不同的 registries 上传或者下载 packages 。比如说,企业版的 npm registry,用于工作相关。而在npmjs.com 公共的 registry,用于 开源代码项目。

这个时候,你就可以使用 npmrc工具 为每一个 registry 创建和管理单独的 npm 用户配置文件(profile)。

概览

  • 安装 npmrc
  • 创建一个 npm 企业版 配置文件(profile)
  • 给公共的 npm registry 创建配置文件
  • 通过 npmrc 切换不同的配置文件

安装 npmrc

在命令行中运行下面的指令

  1. npm i npmrc -g

创建一个 npm 企业版 配置文件(profile)

在安装完 npmrc之后,你可以创建一个 profile 来访问你公司的企业 registry 。

  1. 在命令行中输入 npmrc -c name-of-profile 去创建一个 npm Enterprise profile 。 比如说,创建一个名为 “work” 的配置文件:

    1. npmrc -c work
  2. 在命令行中运行下面的命令去给 profile 设置为 npm 企业 register。用你公司的 npm Enterprise registry的名字替换命令中的 your-company-registry:

    1. npm config set registry https://registry.your-company-registry.npme.io/

    给公共的 npm registry 创建配置文件

在你创建完你的 npm Enterprise profile, 你可以创建为一个不同的 register 创建第二个 profile,比如说:公开 npm register

  1. 在命令行中运行 npmrc -c name-of-profile 来创建一个公共 register 。比如说,去创建一个名为 “open source” 的 profile,那么就运行 npmrc -c open-source
  2. 运行下面的命令,给你的 open source profile 设置成公共register。
    1. npm config set registry https://registry.npmjs.org/

通过 npmrc 切换不同的配置文件

在命令行中,输入下面的命令去选择 profiles 。使用你的 profile 的名字来替换 profile-name

  1. npmrc profile-name

配置 作用域(scopes)去指出(point)不同的 registries

你也可以将不同的作用域映射(map)到不同的 register上。

比如说,要在使用 npm Enterprise registry 作用域 @company-scope 的同时,又要使用公共的 npm register 下载 公共的 packages。那么就可以使用下面的命令,用你的 scope 替换 @compay-scope ,以及使用你公司的 npm Enterprise registry 名字 来替换 company-registry

  1. npm login --scope=@company-scope --registry=https://registry.company-registry.npme.io/