arrow_back

Create an NPM Artifact Registry and Upload Code

로그인 가입
700개 이상의 실습 및 과정 이용하기

Create an NPM Artifact Registry and Upload Code

실습 30분 universal_currency_alt 크레딧 1개 show_chart 입문
info 이 실습에는 학습을 지원하는 AI 도구가 통합되어 있을 수 있습니다.
700개 이상의 실습 및 과정 이용하기

gem-artifact-registry-npm

Google Cloud self-paced labs logo

Activate Cloud Shell

Cloud Shell is a virtual machine that is loaded with development tools. It offers a persistent 5GB home directory and runs on the Google Cloud. Cloud Shell provides command-line access to your Google Cloud resources.

  1. Click Activate Cloud Shell Activate Cloud Shell icon at the top of the Google Cloud console.

When you are connected, you are already authenticated, and the project is set to your PROJECT_ID. The output contains a line that declares the PROJECT_ID for this session:

Your Cloud Platform project in this session is set to YOUR_PROJECT_ID

gcloud is the command-line tool for Google Cloud. It comes pre-installed on Cloud Shell and supports tab-completion.

  1. (Optional) You can list the active account name with this command:
gcloud auth list
  1. Click Authorize.

  2. Your output should now look like this:

Output:

ACTIVE: * ACCOUNT: student-01-xxxxxxxxxxxx@qwiklabs.net To set the active account, run: $ gcloud config set account `ACCOUNT`
  1. (Optional) You can list the project ID with this command:
gcloud config list project

Output:

[core] project = <project_ID>

Example output:

[core] project = qwiklabs-gcp-44776a13dea667a6 Note: For full documentation of gcloud, in Google Cloud, refer to the gcloud CLI overview guide.

Overview

In this lab, you'll learn how to create an NPM Artifact Registry in Google Cloud and upload a simple Node.js package to it. This lab assumes you have basic familiarity with Google Cloud and NPM package management. You will create an Artifact Registry repository for NPM packages, configure your local environment to authenticate with Artifact Registry, create a sample NPM package, and then publish that package to your newly created repository.

Task 1. Create an NPM Artifact Registry Repository

First, you'll create a new Artifact Registry repository specifically for storing NPM packages.

  1. Enable the Artifact Registry API. If you haven't already enabled the Artifact Registry API in your project, you can do so using the following command:
gcloud services enable artifactregistry.googleapis.com Note:
This command enables the Artifact Registry API.
  1. Set your Project ID:
gcloud config set project {{{ project_0.project_id | "PROJECT_ID" }}} Note:
This command sets your active project identity.
  1. Set your default region to
gcloud config set compute/region {{{ project_0.default_region | "REGION" }}} Note:
This command sets your active compute region.
  1. Create a new NPM repository in Artifact Registry. Replace my-npm-repo with your desired repository name.
gcloud artifacts repositories create my-npm-repo \ --repository-format=npm \ --location={{{ project_0.default_region | "REGION" }}} \ --description="NPM repository" Note:
Apply my-npm-repo as a unique name for your repository.

Task 2. Create and Publish an NPM Package

Now, you'll create a simple NPM package to publish to Artifact Registry repository.

  1. Create a new directory for your package and navigate into it:
mkdir my-npm-package cd my-npm-package Note:
Creates a directory named my-npm-package
  1. Initialize a new NPM package. Ensure the package name is unique (e.g. preferably prefixed with your project ID for namespacing) and :
npm init --scope=@{{{ project_0.project_id | "PROJECT_ID" }}} -y Note:
Initializes an npm package with a scope
  1. Create a simple index.js file:
echo 'console.log(`Hello from my-npm-package!`);' > index.js Note:
Creates a basic index.js file

Task 3. Configure NPM to Authenticate with Artifact Registry

Next, configure NPM to authenticate with your Artifact Registry repository. This allows you to publish and install packages.

  1. Create Artifact settings in your .npmrc file (if it does not exist create it). Note that this step generates the .npmrc file.
gcloud artifacts print-settings npm \ --project={{{ project_0.project_id | "PROJECT_ID" }}} \ --repository=my-npm-repo \ --location={{{ project_0.default_region | "REGION" }}} \ --scope=@{{{ project_0.project_id | "PROJECT_ID" }}} > ./.npmrc Note:
This command prints configuration settings for npm and adds a scope. Use this command to generate the formatting required in the `.npmrc` to avoid formatting issues.
  1. Authenticate to Artifact Registry. Run the following command to authenticate:
gcloud auth configure-docker {{{ project_0.default_region | "REGION" }}}-npm.pkg.dev Note:
This command configures Docker to authenticate with Artifact Registry.
  1. Update the package.json file to authenticate to Artifact Registry
{ "name": "@qwiklabs-gcp-00-e9159952f381/my-npm-package", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "artifactregistry-login": "npx google-artifactregistry-auth --repo-config=./.npmrc --credential-config=./.npmrc", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "type": "commonjs" } Note:
Add the artifactregistry-login script into the package.json
  1. Refresh the authentication token by calling the artifactregisty-login
npm run artifactregistry-login Note:
The artifactregistry-login script command will use your active gcloud login to fetch a short-lived access token and insert it into your .npmrc file.
  1. View the .npmrc file to view the authentication token is added
cat .npmrc Note:
A fresh authentication token has been added to the .npmrc file.
  1. Publish the package to Artifact Registry. Replace my-npm-repo with your repository name:
npm publish --registry=https://{{{ project_0.default_region | "REGION" }}}-npm.pkg.dev/{{{ project_0.project_id | "PROJECT_ID" }}}/my-npm-repo/ Note:
The package has been published to the specified Artifact Registry repository.

Task 4. Verify Package Upload

Confirm the package has been successfully uploaded to artifact registry

  1. Navigate to Artifact Registry in the Google Cloud Console.
gcloud artifacts packages list --repository=my-npm-repo --location={{{ project_0.default_region | "REGION" }}} Note:
This command lists NPM packages in the Artifact Registry. You can verify that your package exists.
  1. Verify that your package is listed in the Artifact Registry repository.
PACKAGE: @{{{ project_0.project_id | "PROJECT_ID" }}} CREATE_TIME: 2025-06-25T04:20:04 UPDATE_TIME: 2025-06-25T04:20:04 ANNOTATIONS: Note:
This command lists packages in the Artifact Registry. You can verify that your package exists.

Congratulations!

You have successfully created an NPM Artifact Registry repository and published an NPM package to it. This allows you to manage and share your private NPM packages within your Google Cloud project. Remember to manage access to your repository appropriately to control who can publish and consume packages.

Additional Resources

Manual Last Updated Jun 24, 2025

Lab Last Tested Jun 24, 2025

시작하기 전에

  1. 실습에서는 정해진 기간 동안 Google Cloud 프로젝트와 리소스를 만듭니다.
  2. 실습에는 시간 제한이 있으며 일시중지 기능이 없습니다. 실습을 종료하면 처음부터 다시 시작해야 합니다.
  3. 화면 왼쪽 상단에서 실습 시작을 클릭하여 시작합니다.

시크릿 브라우징 사용

  1. 실습에 입력한 사용자 이름비밀번호를 복사합니다.
  2. 비공개 모드에서 콘솔 열기를 클릭합니다.

콘솔에 로그인

    실습 사용자 인증 정보를 사용하여
  1. 로그인합니다. 다른 사용자 인증 정보를 사용하면 오류가 발생하거나 요금이 부과될 수 있습니다.
  2. 약관에 동의하고 리소스 복구 페이지를 건너뜁니다.
  3. 실습을 완료했거나 다시 시작하려고 하는 경우가 아니면 실습 종료를 클릭하지 마세요. 이 버튼을 클릭하면 작업 내용이 지워지고 프로젝트가 삭제됩니다.

현재 이 콘텐츠를 이용할 수 없습니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

감사합니다

이용할 수 있게 되면 이메일로 알려드리겠습니다.

한 번에 실습 1개만 가능

모든 기존 실습을 종료하고 이 실습을 시작할지 확인하세요.

시크릿 브라우징을 사용하여 실습 실행하기

이 실습을 실행하려면 시크릿 모드 또는 시크릿 브라우저 창을 사용하세요. 개인 계정과 학생 계정 간의 충돌로 개인 계정에 추가 요금이 발생하는 일을 방지해 줍니다.