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.
O GitHub AE está atualmente sob versão limitada. Entre em contato com nossa Equipe de Vendas para saber mais.

Working with the npm registry

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

GitHub Package Registry está disponível com GitHub Free, GitHub Pro, GitHub Free para organizações, GitHub Team, GitHub Enterprise Cloud, GitHub Enterprise Server e GitHub AE.

Observação: GitHub Package Registry está atualmente em beta para GitHub AE.

Limits for published npm versions

If you publish over 1,000 npm package versions to GitHub Package Registry, you may see performance issues and timeouts occur during usage.

In the future, to improve performance of the service, you won't be able to publish more than 1,000 versions of a package on GitHub. Any versions published before hitting this limit will still be readable.

If you reach this limit, consider deleting package versions or contact Support for help. When this limit is enforced, our documentation will be updated with a way to work around this limit. For more information, see "Deleting a package" or "Contacting Support."

Authenticating to GitHub Package Registry

Você precisa de um token de acesso para publicar, instalar e excluir pacotes no GitHub Package Registry.

You can use a personal access token (PAT) to authenticate to GitHub Package Registry or the GitHub AE API. Ao criar um token de acesso pessoal, você pode atribuir diferentes escopos de token, dependendo da sua necessidade. Para obter mais informações sobre os escopos dos pacotes para um PAT, consulte "Sobre permissões para o GitHub Packages."

Para efetuar a autenticação em um registro do GitHub Package Registry dentro de um fluxo de trabalho de GitHub Actions, você pode utilizar:

  • GITHUB_TOKEN para publicar pacotes associados ao repositório do fluxo de trabalho.
  • um PAT para instalar pacotes associados a outros repositórios privados (que o GITHUB_TOKEN não consegue acessar).

Para obter mais informações sobre GITHUB_TOKEN usado nos fluxos de trabalho de GitHub Actions, consulteAutenticação em um fluxo de trabalho".

Authenticating with a personal access token

Você deve usar um token de acesso pessoal com os escopos apropriados para publicar e instalar pacotes no GitHub Package Registry. Para obter mais informações, consulte "Sobre 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 HOSTNAME with the host name of your enterprise and TOKEN with your personal access token. Create a new ~/.npmrc file if one doesn't exist.

//npm.HOSTNAME//:_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.

If GitHub Package Registry is not your default package registry for using npm and you want to use the npm audit command, we recommend you use the --scope flag with the owner of the package when you authenticate to GitHub Package Registry.

$ npm login --scope=@OWNER --registry=https://npm.HOSTNAME/

> Username: USERNAME
> Password: TOKEN
> Email: PUBLIC-EMAIL-ADDRESS

Publishing a package

Note: Package names and scopes must only use lowercase letters.

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."

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 Package Registry 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".

Após publicar um pacote, você poderá visualizá-lo no GitHub. Para obter mais informações, consulte "Visualizar pacotes".

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.

  1. Autenticar para GitHub Package Registry. Para obter mais informações, consulte "Authenticating to GitHub Package Registry."

  2. No mesmo diretório que o arquivo package.json, crie ou edite um arquivo .npmrc para incluir uma linha especificando a URL GitHub Package Registry e o proprietário da conta. Substitua OWNER pelo nome do usuário ou da organização que possui o repositório que contém seu projeto.

    Se o isolamento de subdomínio estiver habilitado:

    @OWNER:registry=https://npm.HOSTNAME

    Se o isolamento de subdomínio estiver desabilitado:

    @OWNER:registry=https://HOSTNAME/_registry/npm
  3. Adicione o arquivo .npmrc ao repositório, onde GitHub Package Registry pode encontrar seu projeto. Para obter mais informações, consulte "Adicionar um arquivo a um repositório."

  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. Verifique o campo repositório no package.json do seu projeto. O campo repositório deve corresponder à URL do seu repositório de GitHub. Por exemplo, se a URL do repositório for github.com/my-org/test, o campo do repositório deverá ser git://github.com/my-org/test.git.

  6. Publicar um pacote:

    $ 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.HOSTNAME/"
    },
  2. Verifique o campo repositório no package.json do seu projeto. O campo repositório deve corresponder à URL do seu repositório de GitHub. Por exemplo, se a URL do repositório for github.com/my-org/test, o campo do repositório deverá ser git://github.com/my-org/test.git.

  3. Publicar um pacote:

    $ 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.

"repository":"https://HOSTNAME/OWNER/REPOSITORY",

Installing a package

You can install packages from GitHub Package Registry 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 that all requests to install packages will be routed to GitHub Package Registry.

By default, you can only use npm packages hosted on your enterprise, and you will not be able to use unscoped packages. For more information on package scoping, see "npm-scope" in the npm documentation. If required, GitHub support can enable an upstream proxy to npmjs.org. Once an upstream proxy is enabled, if a requested package isn't found on your enterprise, GitHub Package Registry makes a proxy request to npmjs.org.

  1. Autenticar para GitHub Package Registry. Para obter mais informações, consulte "Authenticating to GitHub Package Registry."

  2. No mesmo diretório que o arquivo package.json, crie ou edite um arquivo .npmrc para incluir uma linha especificando a URL GitHub Package Registry e o proprietário da conta. Substitua OWNER pelo nome do usuário ou da organização que possui o repositório que contém seu projeto.

    Se o isolamento de subdomínio estiver habilitado:

    @OWNER:registry=https://npm.HOSTNAME

    Se o isolamento de subdomínio estiver desabilitado:

    @OWNER:registry=https://HOSTNAME/_registry/npm
  3. Adicione o arquivo .npmrc ao repositório, onde GitHub Package Registry pode encontrar seu projeto. Para obter mais informações, consulte "Adicionar um arquivo a um repositório."

  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 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"
      }
    }
    
  5. Install the package.

    $ npm install

Installing packages from other organizations

By default, you can only use GitHub Package Registry 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 HOSTNAME with the host name of your enterprise and OWNER with the name of the user or organization account that owns the repository containing your project.

@OWNER:registry=https://npm.HOSTNAME
@OWNER:registry=https://npm.HOSTNAME

Further reading

Esse documento ajudou você?

Política de Privacidade

Ajude-nos a tornar esses documentos ótimos!

Todos os documentos do GitHub são de código aberto. Você percebeu que algo que está errado ou não está claro? Envie um pull request.

Faça uma contribuição

Ou, aprenda como contribuir.