Store Node.js packages in Artifact Registry
This quickstart shows you how to set up an Artifact Registry Node.js package repository and upload a package to it.
Before you begin
- 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.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Artifact Registry API.
- Install and initialize the Google Cloud CLI.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
-
Make sure that billing is enabled for your Cloud project. Learn how to check if billing is enabled on a project.
-
Enable the Artifact Registry API.
- Install and initialize the Google Cloud CLI.
- 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:
Go to Google Cloud console.
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.
Create the repository.
Console
Open the Repositories page in the Google Cloud console.
Click Create Repository.
Specify
quickstart-nodejs-repo
as the repository name.Choose npm as the format.
Under Location Type, select Region and then choose the location
us-central1
.Click Create.
The repository is added to the repository list
gcloud
Run the following command to create a new Node.js package repository named
quickstart-nodejs-repo
in the locationus-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"
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
.To simplify
gcloud
commands, set the default repository toquickstart-nodejs-repo
and the default location tous-central1
. After the values are set, you do not need to specify them ingcloud
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
Create an npm package to upload. If you don't have a package available to use with this quickstart, run the following command to create a plain
package.json
file:npm init -y
Configure your npm project to deploy to the Artifact Registry repository that you created.
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 this:@quickstart:registry=https://us-central1-npm.pkg.dev/PROJECT/quickstart-nodejs-repo/ //us-central1-npm.pkg.dev/PROJECT/quickstart-nodejs-repo/:always-auth=true
Add the configuration settings from the previous step to your project
.npmrc
file. The file is usually in the same directory as thepackage.json
file.Replace PROJECT with your project ID.
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.
Upload a package to the repository
Choose a package to upload. Ensure that package name in
package.json
includes thequickstart
scope."name": "@quickstart/my-package"
Refresh the access token for connecting to the repository. google-artifactregistry-auth is a client library that updates credentials for Artifact Registry repositories.
Add a script to the
package.json
file in your project."scripts": { "artifactregistry-login": "npx google-artifactregistry-auth" }
Run the script with the command:
npm run artifactregistry-login
Add your package to the repository. You can use an
npm
oryarn
command.npm publish
yarn publish
View the package in the repository
To verify that your package was added to the repository:
Console
Open the Repositories page in the Google Cloud console.
In the repository list, click the
quickstart-nodejs-repo
repository.The Packages page lists the packages in the repository.
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
To install a package from the Node.js package repository, use the npm install
or
yarn add
command. Replace PACKAGE with your package name.
npm install @quickstart/PACKAGE
yarn add @quickstart/PACKAGE
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
Open the Repositories page in the Google Cloud console.
In the repository list, select the
quickstart-nodejs-repo
repository.Click Delete.
gcloud
To delete the
quickstart-nodejs-repo
repository, run the following command:gcloud artifacts repositories delete quickstart-nodejs-repo
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
- Learn more about configuring authentication
- Learn about managing repositories
- Learn about managing packages
- Read our resources about DevOps and explore our research program.