Store Node.js packages in Artifact Registry

This quickstart shows you how to set up a private Artifact Registry Node.js package repository and upload a package to it.

Before you begin

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project .

  4. Enable the Artifact Registry API.

    Enable the API

  5. Install the Google Cloud CLI.

  6. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .

  7. To initialize the gcloud CLI, run the following command:

    gcloud  
    init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project .

  10. Enable the Artifact Registry API.

    Enable the API

  11. Install the Google Cloud CLI.

  12. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity .

  13. To initialize the gcloud CLI, run the following command:

    gcloud  
    init
  14. Install PowerShell, if you want to connect to a Node.js package repository from Windows.

Launch Cloud Shell

In this quickstart, you will use Cloud Shell , which is a shell environment for managing resources hosted on Google Cloud.

Cloud Shell comes preinstalled with the Google Cloud CLI . The gcloud CLI provides the primary command-line interface for Google Cloud.

Launch Cloud Shell:

  1. Go to Google Cloud console.

    Google Cloud console

  2. On the Google Cloud console toolbar, click Activate Cloud Shell:

A Cloud Shell session opens inside a frame lower on the console. You use this shell to run gcloud commands.

Create a Node.js package repository

Create the repository for your Node.js artifacts.

  1. Create the repository.

    Console

    1. Open the Repositoriespage in the Google Cloud console.

      Open the Repositories page

    2. Click Create Repository.

    3. Specify quickstart-nodejs-repo as the repository name.

    4. Choose npmas the format and Standardas the mode.

    5. Under Location Type, select Regionand then choose the location us-central1 .

    6. Click Create.

    The repository is added to the repository list

    gcloud

    1. Run the following command to create a new Node.js package repository named quickstart-nodejs-repo in the location us-central1 with the description "Node.js repository".

       gcloud  
      artifacts  
      repositories  
      create  
      quickstart-nodejs-repo  
      --repository-format = 
      npm  
       \ 
      --location = 
      us-central1  
      --description = 
       "Node.js package repository" 
       
      
    2. Run the following command to verify that your repository was created.

       gcloud  
      artifacts  
      repositories  
      list 
      

    For more information about Artifact Registry commands, run the command gcloud artifacts .

  2. To simplify gcloud commands, set the default repository to quickstart-nodejs-repo and the default location to us-central1 . After the values are set, you do not need to specify them in gcloud commands that require a repository or a location.

    To set the repository, run the command:

     gcloud  
    config  
     set 
      
    artifacts/repository  
    quickstart-nodejs-repo 
    

    To set the location, run the command:

     gcloud  
    config  
     set 
      
    artifacts/location  
    us-central1 
    

    For more information about these commands, see the gcloud config set documentation.

Configure npm

To upload and download packages, you must configure your npm projects with settings to authenticate with the repository you created. For this quickstart, you'll upload a simple package and then install it in a second package as a dependency.

  1. Download the emoji-regex package, a simple package that provides a regular expression to match all emoji symbols and sequences.

     npm  
    pack  
    emoji-regex 
    

    The command downloads an archive of the package.

  2. Extract the archive into an emoji-regex directory. The following command is for an archive of emoji-regex version 10.1.0:

     mkdir  
    emoji-regex && 
    tar  
    xvf  
    emoji-regex-10.1.0.tgz  
    -C  
    emoji-regex  
    --strip-components  
     1 
     
    
  3. Create a second npm package that you'll use to install the emoji-regex package from Artifact Registry. For this package, use the npm init command to create a basic package.

     mkdir  
    npm-package2 cd 
      
    npm-package2
    
    npm  
    init  
    -y 
    

    When prompted, accept default values.

  4. Configure both the emoji-regex and npm-package2 projects to authenticate with the Artifact Registry repository that you created.

    1. Run the following command. The command returns configuration settings to add to your npm configuration file.

       gcloud  
      artifacts  
      print-settings  
      npm  
      --scope = 
      @quickstart 
      

      The quickstart scope is associated with your repository. When you include the scope in commands to publish or install packages, npm uses your repository. When you publish or install packages without a scope, your configured default repository is used. For more information, see the Node.js overview .

      The output of the gcloud command looks like the following example. PROJECT is your Google Cloud project ID.

       @quickstart:registry = 
      https://us-central1-npm.pkg.dev/ PROJECT 
      /quickstart-nodejs-repo/
      //us-central1-npm.pkg.dev/ PROJECT 
      /quickstart-nodejs-repo/:always-auth = 
       true 
       
      
    2. Add the configuration settings from the previous step to the project .npmrc file in each npm project. The file in the same directory as the package.json file.

      To learn more about the .npmrc file, see the Node.js overview .

      Each Artifact Registry Node.js package repository is associated with an npm registry endpoint https:// LOCATION -npm.pkg.dev/ PROJECT / REPOSITORY . If you create another Artifact Registry Node.js package repository, npm interacts with it as a separate registry with its own scope.

  5. Edit package.json in both the emoji-regex and npm-package2 projects.

    1. Ensure that the value for name includes the quickstart scope.

      • For emoji-regex :
        "name" 
       : 
        
       "@quickstart/emoji-regex" 
       
      
      • For npm-package2 :
        "name" 
       : 
        
       "@quickstart/npm-package2" 
       
      
    2. Under scripts , add a script for google-artifactregistry-auth , a client library that updates credentials for Artifact Registry repositories.

        "scripts" 
       : 
        
       { 
        
       "artifactregistry-login" 
       : 
        
       "npx google-artifactregistry-auth --repo-config=./.npmrc --credential-config=./.npmrc" 
       } 
       
      

Upload a package to the repository

  1. Refresh the access token for connecting to the repository.

     npm  
    run  
    artifactregistry-login 
    
  2. Add your package to the repository. You can use an npm or yarn command.

     npm  
    publish 
    
     yarn  
    publish 
    

View the package in the repository

To verify that your package was added to the repository:

Console

  1. Open the Repositoriespage in the Google Cloud console.

    Open the Repositories page

  2. In the repository list, click the quickstart-nodejs-repo repository.

    The Packagespage lists the packages in the repository.

  3. Click on a package to view package versions.

gcloud

To list the images in the default quickstart-nodejs-repo repository, run the following command:

   
 gcloud 
  
 artifacts 
  
 packages 
  
 list 
 

To view versions for a package, run the following command:

 gcloud  
artifacts  
versions  
list  
--package = 
 PACKAGE 
 

Where PACKAGE is the package ID.

Installing packages

Install the emoji-regex package from your Artifact Registry repository in npm-package2 .

In the npm-package2 directory, run the npm install or yarn add command:

 npm  
install  
@quickstart/emoji-regex 
 yarn  
add  
@quickstart/emoji-regex 

The command installs the emoji-regex package in the npm-package2 project. It also updates package.json to set emoji-regex as a dependency.

  "dependencies" 
 : 
  
 { 
  
 "@quickstart/emoji-regex" 
 : 
  
 "^10.1.0" 
 } 
 

Clean up

To avoid incurring charges to your Google Cloud account for the resources used on this page, follow these steps.

Before you remove a repository, ensure that any packages that you want to keep are available in another location.

To delete the repository:

Console

  1. Open the Repositoriespage in the Google Cloud console.

    Open the Repositories page

  2. In the repository list, select the quickstart-nodejs-repo repository.

  3. Click Delete.

gcloud

  1. To delete the quickstart-nodejs-repo repository, run the following command:

     gcloud  
    artifacts  
    repositories  
    delete  
    quickstart-nodejs-repo 
    
  2. If you want to remove the default repository and location settings that you configured for the active gcloud configuration, run the following commands:

     gcloud  
    config  
     unset 
      
    artifacts/repository
    gcloud  
    config  
     unset 
      
    artifacts/location 
    

What's next

Design a Mobile Site
View Site in Mobile | Classic
Share by: