如何在本地设置多个 NPM 用户? [英] How to have multiple NPM users set up locally?

查看:67
本文介绍了如何在本地设置多个 NPM 用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了两个 NPM 帐户:一个公共帐户和一个私人帐户.我将如何设置它以便每次我在不同的帐户上发布模块时都不需要 npm login?

I am using two NPM accounts: a public one and a private one. How would I set it up so that I don't need to npm login every time I publish a module on a different account?

UPD:寻找 NPM 内置解决方案,所以没有 shell 脚本之类的

UPD: Looking for an NPM inbuilt solution, so no shell scripts or the like

推荐答案

这就是我如何解决它有 4 个不同的 NPM 登录.

This is how I'm solving it having 4 different NPM logins.

  1. 在每个项目的 .gitignore(以及 NPM 模块的 .npmignore)中添加这一行:.npmrc.这将确保您永远不会提交(或发布).npmrc 文件.
  2. 在每个项目的文件夹中创建 .npmrc 文件,其中包含://registry.npmjs.org/:_authToken=11111111-1111-1111-1111-111111111111(替换带有实际 NPM 身份验证令牌的 GUID,例如,您可以从 ~/.npmrc) 中获取它
  1. In each project's .gitignore (and .npmignore for NPM modules) add this line: .npmrc. This will make sure you never commit (or publish) the .npmrc file.
  2. In each project's folder create .npmrc file containing this: //registry.npmjs.org/:_authToken=11111111-1111-1111-1111-111111111111 (replace the GUID with an actual NPM auth token, e.g. you can grab it from ~/.npmrc)

npm CLI 将在您当前文件夹中查找 .npmrc 文件(或任何 文件夹),并将其用于授权.

The npm CLI will look in your current folder for the .npmrc file (or in any parent folder) and will use it for auth.

结果所有 npm 命令都按原样工作,不需要传递 --userconfig 或任何东西.

As the result all npm commands work as is, no need to pass --userconfig or anything.

除上述之外,您还可以在您的计算机/笔记本电脑上使用 默认 NPM 令牌.

In addition to the above you can have the default NPM token across your computer/laptop.

  1. 确保 .npmrc 不存在于 .gitignore(这在大多数项目中很常见).
  2. 在项目的根文件夹中创建 .npmrc 文件.把它放在里面://registry.npmjs.org/:_authToken=${NPM_TOKEN}.这将使 npm 使用 NPM_TOKEN 环境.变种如果这样的环境,npm 将中止.变种未找到.
  3. 提交并推送该文件.(是的.说真的.)
  4. 确保您的 shell 设置了 NPM_TOKEN 环境变量.例如.NPM_TOKEN=11111111-1111-1111-1111-111111111111.我的 ~/.bash_profile 中有它.
  1. Make sure .npmrc is NOT present in .gitignore (which is common for most projects out there).
  2. Create the .npmrc file in the root folder of your project. Put this inside: //registry.npmjs.org/:_authToken=${NPM_TOKEN}. This will make npm to use NPM_TOKEN env. var. And npm will abort if such env. var. is not found.
  3. Commit and push that file. (Yes. Seriously.)
  4. Make sure your shell has the NPM_TOKEN environment variable set. E.g. NPM_TOKEN=11111111-1111-1111-1111-111111111111. I have it in my ~/.bash_profile.

已提交此文件的所有项目都将使用您的环境变量 NPM_TOKEN 进行 npm 身份验证.

All the projects, which have this file committed, will use your environment variable NPM_TOKEN for npm auth.

结果所有 npm 命令都按原样工作,不需要传递 --userconfig 或任何东西.

As the result all npm commands work as is, no need to pass --userconfig or anything.

这对于 CI(持续集成)来说已经足够安全了.所有 CI-s 都允许您设置环境变量.使用这种方法,您可以使用简单的 env 更改 NPM 用户.变种改变.

This is good and secure enough for CI (Continuous Integration). All CI-s allow you to set environment variables. Using this approach you can change the NPM user with a simple env. var. change.

专业提示

输入 npm whoami 命令以检查文件夹中当前正在使用哪个令牌.

Type npm whoami command to check which token is currently being used in the folder.

这篇关于如何在本地设置多个 NPM 用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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