要与一组有限的用户或团队共享你的代码,你可以将私有用户范围或组织范围的包发布到 npm 注册表。

🌐 To share your code with a limited set of users or teams, you can publish private user-scoped or organization-scoped packages to the npm registry.

有关作用域和私有包的更多信息,请参见 "关于范围" 和 "关于私有包"。

🌐 For more information on scopes and private packages, see "About scopes" and "About private packages".

注意: 在你发布私有用户作用域的 npm 包之前,你必须注册一个付费的 npm 用户账户。

此外,要发布面向私有组织的包,你必须先创建一个 npm 用户账户,然后创建一个付费的 npm 组织

🌐 Additionally, to publish private organization-scoped packages, you must create an npm user account, then create a paid npm organization.

创建私有包

🌐 Creating a private package

  1. 如果你正在使用 npmrc 来 在多个注册表上管理账户,在命令行中,切换到相应的配置文件:

    npmrc <profile-name>
  2. 在命令行上,为你的包创建一个目录:

    mkdir my-test-package
  3. 导航到包的根目录:

    cd my-test-package
  4. 如果你使用 git 来管理你的包代码,在包的根目录下运行以下命令,并将 git-remote-url 替换为你的包的 git 远程 URL:

    git init
    git remote add origin git://git-remote-url
  5. 在包的根目录中,运行 npm init 命令,并将作用域传递给 scope 标志:

    • 对于面向组织的包,将 my-org 替换为你的组织名称:

      npm init --scope=@my-org
    • 对于用户范围的包,将 my-username 替换为你的用户名:

      npm init --scope=@my-username
  6. 根据提示生成package.json文件。有关包命名的帮助,请参见"包名指南"。

  7. 创建一个 自述文件 来解释你的软件包代码是什么以及如何使用它。

  8. 在你首选的文本编辑器中,为你的包编写代码。

查看包内容是否有敏感或不必要的信息

🌐 Reviewing package contents for sensitive or unnecessary information

将敏感信息发布到注册表可能会对你的用户造成伤害,危及你的开发基础设施,修复成本高昂,并使你面临法律风险。我们强烈建议在将软件包发布到注册表之前删除敏感信息,例如私钥、密码、个人可识别信息(个人身份信息)和信用卡数据。 即使你的软件包是私有的,如果软件包被公开或下载到可能被更多用户访问的计算机上,敏感信息仍可能被泄露。

🌐 Publishing sensitive information to the registry can harm your users, compromise your development infrastructure, be expensive to fix, and put you at risk of legal action. We strongly recommend removing sensitive information, such as private keys, passwords, personally identifiable information (PII), and credit card data before publishing your package to the registry. Even if your package is private, sensitive information can be exposed if the package is made public or downloaded to a computer that can be accessed by more users than intended.

对于不太敏感的信息,例如测试数据,请使用 .npmignore.gitignore 文件以防止发布到注册表。更多信息,请参见 这篇文章

🌐 For less sensitive information, such as testing data, use a .npmignore or .gitignore file to prevent publishing to the registry. For more information, see this article.

测试你的包

🌐 Testing your package

为了减少发布错误的可能性,我们建议在将包发布到 npm 注册表之前先测试你的包。要测试你的包,请使用包含包目录完整路径的命令 npm install:

🌐 To reduce the chances of publishing bugs, we recommend testing your package before publishing it to the npm registry. To test your package, run npm install with the full path to your package directory:

npm install my-package

发布私有包

🌐 Publishing private packages

默认情况下,范围包以私有可见性发布。

🌐 By default, scoped packages are published with private visibility.

重要: 发布到 npm 需要以下条件之一:

  • 你的账户已启用双重身份验证 (2FA),或者
  • 启用绕过双重身份验证 (2FA) 的细粒度访问令牌

欲了解更多信息,请参阅 npm 关于发布包时要求两步验证的文档。

🌐 For more information, see the npm documentation on requiring 2FA for package publishing.

  1. 在命令行上,导航到包的根目录。

    cd /path/to/package
  2. 要将你的私有包发布到 npm 注册表,请运行:

    npm publish
  3. 要查看你的私有包页面,请访问 https://npmjs.com/package/package-name,将 package-name 替换为你的包名称。私有包在 npm 网站上的包名称下方会显示 private

    Screenshot of a private npm Teams package

有关 publish 命令的更多信息,请参阅 命令行接口文档

🌐 For more information on the publish command, see the CLI documentation.