基于目录切换 npm 注册表 [英] switching npm registry based on directory

查看:39
本文介绍了基于目录切换 npm 注册表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始为 node 开发.我工作的公司有一个内部 npm 注册表.我想知道如何根据我的开发位置使用不同的注册表设置.为了说明,我有一个如下所示的目录结构:

<代码>~/代码||-- 我的项目||||-- 项目 1||-- 项目 2||-- 我的公司||--项目1|--项目二

当我在我的项目"中的一个项目中进行开发时,我希望 npm 注册表指向 https://registry.npmjs.org(默认注册表).但是,当我在我的公司"中的一个项目中进行开发时,我希望 npm 注册表指向公司特定的注册表.现在,我天真的方法是使用npm config set registry 更新注册表.

解决方案

使用私有 npm 注册表有两种不同的用例:

  1. 安装:使用 --reg--registry 标志:

    npm install mypackage --reg http://myreg.npmjitsu.com

  2. 发布:您也可以使用 --reg--registry,但我强烈建议您使用publishConfig 在您的 package.json 文件中设置(参见:npm 文档)

    <代码>{发布配置":{注册表":http://myreg.npmjitsu.com"}}

    这可以防止开发人员通过忘记 --registry 标志

  3. 而意外地将私有模块发布到公共注册中心

因此,将 publishConfig 添加到所有 package.json 中,以便您的公司在从其私有 npm 安装时使用 --registry.

I have recently started developing for node. The company that I work for has an internal npm registry. I want to know how to use different registry settings depending upon where I am developing. To illustrate, I have a directory structure like shown below:

~/Code
|
| -- My Projects
| |
| | -- Proj 1 
| | -- Proj 2
|
|-- My Company
  |
  |--Proj 1 
  |--Proj 2

When I am developing in one of the projects in 'My Project', I would like the npm registry to point to https://registry.npmjs.org (the default registry). But when I am developing in one of the projects in 'My Company', I want npm registry to point to the company specific registry. Right now, my naive method is to use npm config set registry to update the registry.

解决方案

There are two distinct use cases for using your private npm registry:

  1. Installing: use the --reg or --registry flag:

    npm install mypackage --reg http://myreg.npmjitsu.com
    

  2. Publishing: you can also use --reg or --registry, but I would highly recommend that you use the publishConfig setting in your package.json file (See: the npm documentation)

    {
      "publishConfig": {
        "registry": "http://myreg.npmjitsu.com"
      }
    }
    

    This prevents developers from accidentally publishing private modules to the public registry by forgetting the --registry flag

So add publishConfig to all of the package.json for your Company use --registry when installing from their private npm.

这篇关于基于目录切换 npm 注册表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆