我们经常发布文档更新,此页面的翻译可能仍在进行中。有关最新信息,请访问英文文档。如果此页面上的翻译有问题,请告诉我们

Configuring npm for use with GitHub Packages

You can configure npm to publish packages to GitHub Packages and to use packages stored on GitHub Packages as dependencies in an npm project.

GitHub Packages 可用于 GitHub Free、GitHub Pro、组织的 GitHub Free、GitHub Team、GitHub Enterprise Cloud、GitHub Enterprise Server 2.22 和 GitHub One。


GitHub Packages 不适用于使用旧版按仓库计划的帐户所拥有的私有仓库。 GitHub Packages 可用于 GitHub Free、GitHub Pro、组织的 GitHub Free、GitHub Team、GitHub Enterprise Cloud 和 GitHub One。 For more information, see "[GitHub's products](/articles/github-s-products)."

本文内容

此文档对您有帮助吗?

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或, 了解如何参与。

Authenticating to GitHub Packages

您需要访问令牌才能发布、安装和删除 GitHub Packages 中的包。 您可以使用个人访问令牌直接向 GitHub Packages 或 GitHub API 验证您的用户名。 创建个人访问令牌时,可根据需要为令牌分配不同的作用域。

To authenticate using a GitHub Actions 工作流程:

  • 对于包注册表 (PACKAGE-REGISTRY.pkg.github.com/OWNER/REPOSITORY/IMAGE-NAME),您可以使用 GITHUB_TOKEN
  • 对于容器注册表 (ghcr.io/OWNER/IMAGE-NAME),必须使用个人访问令牌。

Authenticating with a personal access token

您必须使用具有适当范围的个人访问令牌才可在 GitHub Packages 中发布和安装。 更多信息请参阅“关于 GitHub Packages”。

You can authenticate to GitHub Packages with npm by either editing your per-user ~/.npmrc file to include your personal access token or by logging in to npm on the command line using your username and personal access token.

To authenticate by adding your personal access token to your ~/.npmrc file, edit the ~/.npmrc file for your project to include the following line, replacing TOKEN with your personal access token. Create a new ~/.npmrc file if one doesn't exist.

//npm.pkg.github.com/:_authToken=TOKEN

To authenticate by logging in to npm, use the npm login command, replacing USERNAME with your GitHub username, TOKEN with your personal access token, and PUBLIC-EMAIL-ADDRESS with your email address.

$ npm login --registry=https://npm.pkg.github.com
> Username: USERNAME
> Password: TOKEN
> Email: PUBLIC-EMAIL-ADDRESS

Authenticating with the GITHUB_TOKEN

如果您使用的是 GitHub Actions 工作流程,可以使用 GITHUB_TOKEN 发布和使用 GitHub Packages 中的软件包,而无需存储和管理个人访问令牌。 更多信息请参阅“使用 GITHUB_TOKEN 验证身份”。

Publishing a package

By default, GitHub Packages publishes a package in the GitHub repository you specify in the name field of the package.json file. For example, you would publish a package named @my-org/test to the my-org/test GitHub repository. You can add a summary for the package listing page by including a README.md file in your package directory. For more information, see "Working with package.json" and "How to create Node.js Modules" in the npm documentation.

You can publish multiple packages to the same GitHub repository by including a URL field in the package.json file. For more information, see "Publishing multiple packages to the same repository."

You can set up the scope mapping for your project using either a local .npmrc file in the project or using the publishConfig option in the package.json. GitHub Packages only supports scoped npm packages. Scoped packages have names with the format of @owner/name. Scoped packages always begin with an @ symbol. You may need to update the name in your package.json to use the scoped name. For example, "name": "@codertocat/hello-world-npm".

在发布包后,您可以在 GitHub 上查看该包。 更多信息请参阅“查看包”。

Publishing a package using a local .npmrc file

You can use an .npmrc file to configure the scope mapping for your project. In the .npmrc file, use the GitHub Packages URL and account owner so GitHub Packages knows where to route package requests. Using an .npmrc file prevents other developers from accidentally publishing the package to npmjs.org instead of GitHub Packages. 由于不支持大写字母,因此,即使您的 GitHub 用户或组织名称中包含大写字母,也必须对仓库所有者使用小写字母。

  1. 向 GitHub Packages 验证。 更多信息请参阅“向 GitHub Packages 验证”。

  2. 在与 package.json 文件相同的目录中,创建或编辑 .npmrc 文件以包含指定 GitHub Packages URL 和帐户所有者的行。 将 OWNER 替换为拥有项目所在仓库的用户或组织帐户的名称。

    registry=https://npm.pkg.github.com/OWNER
  3. .npmrc 文件添加到 GitHub Packages 可在其中找到您的项目的仓库。 更多信息请参阅“使用命令行添加文件到仓库”。

  4. Verify the name of your package in your project's package.json. The name field must contain the scope and the name of the package. For example, if your package is called "test", and you are publishing to the "My-org" GitHub organization, the name field in your package.json should be @my-org/test.

  5. 在项目的 package.json 中验证 repository(仓库)字段。 repository(仓库)字段必须匹配 GitHub 仓库的 URL。 例如,如果您的仓库 URL 是 github.com/my-org/test,则仓库字段应为 git://github.com/my-org/test.git

  6. 发布包:

    $ npm publish

Publishing a package using publishConfig in the package.json file

You can use publishConfig element in the package.json file to specify the registry where you want the package published. For more information, see "publishConfig" in the npm documentation.

  1. Edit the package.json file for your package and include a publishConfig entry.

    "publishConfig": {
      "registry":"https://npm.pkg.github.com"
    },
  2. 在项目的 package.json 中验证 repository(仓库)字段。 repository(仓库)字段必须匹配 GitHub 仓库的 URL。 例如,如果您的仓库 URL 是 github.com/my-org/test,则仓库字段应为 git://github.com/my-org/test.git

  3. 发布包:

    $ npm publish

Publishing multiple packages to the same repository

To publish multiple packages to the same repository, you can include the URL of the GitHub repository in the repository field of the package.json file for each package.

To ensure the repository's URL is correct, replace REPOSITORY with the name of the repository containing the package you want to publish, and OWNER with the name of the user or organization account on GitHub that owns the repository.

GitHub Packages will match the repository based on the URL, instead of based on the package name. If you store the package.json file outside the root directory of your repository, you can use the directory field to specify the location where GitHub Packages can find the package.json files.

"repository" : {
    "type" : "git",
    "url": "ssh://git@github.com/OWNER/REPOSITORY.git",
    "directory": "packages/name"
  },

Installing a package

You can install packages from GitHub Packages by adding the packages as dependencies in the package.json file for your project. For more information on using a package.json in your project, see "Working with package.json" in the npm documentation.

By default, you can add packages from one organization. For more information, see "Installing packages from other organizations."

You also need to add the .npmrc file to your project so all requests to install packages will go through GitHub Packages. When you route all package requests through GitHub Packages, you can use both scoped and unscoped packages from npmjs.com. For more information, see "npm-scope" in the npm documentation.

  1. 向 GitHub Packages 验证。 更多信息请参阅“向 GitHub Packages 验证”。

  2. 在与 package.json 文件相同的目录中,创建或编辑 .npmrc 文件以包含指定 GitHub Packages URL 和帐户所有者的行。 将 OWNER 替换为拥有项目所在仓库的用户或组织帐户的名称。

    registry=https://npm.pkg.github.com/OWNER
  3. .npmrc 文件添加到 GitHub Packages 可在其中找到您的项目的仓库。 更多信息请参阅“使用命令行添加文件到仓库”。

  4. Configure package.json in your project to use the package you are installing. To add your package dependencies to the package.json file for GitHub Packages, specify the full-scoped package name, such as @my-org/server. For packages from npmjs.com, specify the full name, such as @babel/core or @lodash. For example, this following package.json uses the @octo-org/octo-app package as a dependency.

    {
      "name": "@my-org/server",
      "version": "1.0.0",
      "description": "Server app that uses the @octo-org/octo-app package",
      "main": "index.js",
      "author": "",
      "license": "MIT",
      "dependencies": {
        "@octo-org/octo-app": "1.0.0"
      }
    }
    
  5. Install the package.

    $ npm install

Installing packages from other organizations

By default, you can only use GitHub Packages packages from one organization. If you'd like to route package requests to multiple organizations and users, you can add additional lines to your .npmrc file, replacing OWNER with the name of the user or organization account that owns the repository containing your project. 由于不支持大写字母,因此,即使您的 GitHub 用户或组织名称中包含大写字母,也必须对仓库所有者使用小写字母。

registry=https://npm.pkg.github.com/OWNER
@OWNER:registry=npm.pkg.github.com
@OWNER:registry=npm.pkg.github.com

Further reading

此文档对您有帮助吗?

帮助我们创建出色的文档!

所有 GitHub 文档都是开源的。看到错误或不清楚的内容了吗?提交拉取请求。

做出贡献

或, 了解如何参与。