👋 We've unified all of GitHub's product documentation in one place! Check out the content for REST API, GraphQL API, and Developers. Learn more on the GitHub blog.


Publicamos atualizações frequentes em nossa documentação, e a tradução desta página ainda pode estar em andamento. Para obter as informações mais recentes, acesse a documentação em inglês. Se houver problemas com a tradução desta página, entre em contato conosco.

Configuring npm for use with GitHub Packages

Você pode configurar o npm para publicar pacotes no GitHub Package Registry e usar pacotes armazenados no GitHub Package Registry como dependências em um projeto npm.

GitHub Package Registry está disponível com GitHub Free, GitHub Pro, GitHub Free para organizações, GitHub Team, GitHub Enterprise Cloud e GitHub One. O GitHub Package Registry não está disponível para repositórios privados de contas que utilizam planos antigos por-repositório. Para obter mais informações, consulte os "produtos do GitHub".

Neste artigo

Autenticar-se no GitHub Package Registry

You need an access token to publish, install, and delete packages in GitHub Package Registry. You can use a personal access token to authenticate with your username directly to GitHub Package Registry or the GitHub API. You can use a GITHUB_TOKEN to authenticate using a GitHub Actions workflow.

Authenticating with a personal access token

You must use a personal access token with the appropriate scopes to publish and install packages in GitHub Package Registry. For more information, see "About GitHub Package Registry."

You can authenticate to GitHub Package Registry 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

If you are using a GitHub Actions workflow, you can use a GITHUB_TOKEN to publish and consume packages in GitHub Package Registry without needing to store and manage a personal access token. For more information, see "Authenticating with the GITHUB_TOKEN."

Publicar um pacote

By default, GitHub Package Registry 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."

É possível definir o mapeamento do escopo para o seu projeto usando um arquivo local .npmrc no projeto ou usando a opção publishConfig em package.json. GitHub Package Registry only supports scoped npm packages. Pacotes com escopo têm nomes no formato @owner/name. Os pacotes com escopo sempre começam pelo símbolo @. Talvez seja necessário atualizar o nome no package.json para usar o nome com escopo. Por exemplo, "name": "@codertocat/hello-world-npm".

After you publish a package, you can view the package on GitHub. For more information, see "Viewing packages."

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 Package Registry URL and account owner so GitHub Package Registry knows where to route package requests. Using an .npmrc file prevents other developers from accidentally publishing the package to npmjs.org instead of GitHub Package Registry. Como não é permitido usar letras maiúsculas, é preciso usar letras minúsculas no nome do proprietário do repositório, mesmo que o nome do usuário ou da organização no GitHub contenha letras maiúsculas.

  1. Authenticate to GitHub Package Registry. Para obter mais informações, consulte "Autenticar-se no GitHub Package Registry."

  • In the same directory as your package.json file, create or edit an .npmrc file to include a line specifying GitHub Package Registry URL and the account owner. Replace OWNER with the name of the user or organization account that owns the repository containing your project.

    registry=https://npm.pkg.github.com/OWNER
  • Add the .npmrc file to the repository where GitHub Package Registry can find your project. For more information, see "Adding a file to a repository using the command line."

  • Verifique o nome do pacote no package.json do seu projeto. O campo name (nome) deve conter o escopo e o nome do pacote. Por exemplo, se o pacote for chamado de "test" e você estiver publicando na organização "My-org" do GitHub, o campo name (nome) do seu package.json deverá ser @my-org/test.

  • Verify the repository field in your project's package.json. The repository field must match the URL for your GitHub repository. For example, if your repository URL is github.com/my-org/test then the repository field should be git://github.com/my-org/test.git.

  • Publish the package:

    $ 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. Para obter mais informações, consulte "publishConfig" na documentação npm.

    1. Edite o arquivo package.json do seu pacote e inclua uma entrada publishConfig.

        "publishConfig": {
          "registry":"https://npm.pkg.github.com/"
        },
      
    2. Verify the repository field in your project's package.json. The repository field must match the URL for your GitHub repository. For example, if your repository URL is github.com/my-org/test then the repository field should be git://github.com/my-org/test.git.

    3. Publish the package:

      $ 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 Package Registry 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 Package Registry can find the package.json files.

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

    Instalar um pacote

    You can install packages from GitHub Package Registry by adding the packages as dependencies in the package.json file for your project. Para obter mais informações sobre como usar um pacote package.json no projeto, consulte "Trabalhar com package.json" na documentação do npm.

    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 Package Registry. When you route all package requests through GitHub Package Registry, you can use both scoped and unscoped packages from npmjs.com. Para obter mais informações, consulte "npm-scope" na documentação npm.

    1. Authenticate to GitHub Package Registry. Para obter mais informações, consulte "Autenticar-se no GitHub Package Registry."

  • In the same directory as your package.json file, create or edit an .npmrc file to include a line specifying GitHub Package Registry URL and the account owner. Replace OWNER with the name of the user or organization account that owns the repository containing your project.

    registry=https://npm.pkg.github.com/OWNER
  • Add the .npmrc file to the repository where GitHub Package Registry can find your project. For more information, see "Adding a file to a repository using the command line."

  • 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 Package Registry, 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"
      }
    }
    
  • Instale o pacote.

    $ npm install
  • Instalar pacotes de outras organizações

    Por padrão, você só pode usar pacotes do GitHub Package Registry de uma organização. 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. Como não é permitido usar letras maiúsculas, é preciso usar letras minúsculas no nome do proprietário do repositório, mesmo que o nome do usuário ou da organização no GitHub contenha letras maiúsculas.

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

    Leia mais

    Pergunte a uma pessoa

    Não consegue encontrar o que procura?

    Entrar em contato