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
- 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 Google Cloud project.
-
Enable the Artifact Registry API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
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 Google Cloud project.
-
Enable the Artifact Registry API.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
- 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 and Standard as the mode.
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
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.
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.
Extract the archive into an
emoji-regex
directory. The following command is for an archive ofemoji-regex
version 10.1.0:mkdir emoji-regex && tar xvf emoji-regex-10.1.0.tgz -C emoji-regex --strip-components 1
Create a second npm package that you'll use to install the
emoji-regex
package from Artifact Registry. For this package, use thenpm init
command to create a basic package.mkdir npm-package2 cd npm-package2 npm init -y
When prompted, accept default values.
Configure both the
emoji-regex
andnpm-package2
projects to authenticate with 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 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
Add the configuration settings from the previous step to the project
.npmrc
file in each npm project. The file in the same directory as thepackage.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.
Edit
package.json
in both theemoji-regex
andnpm-package2
projects.Ensure that the value for
name
includes thequickstart
scope.- For
emoji-regex
:
"name": "@quickstart/emoji-regex"
- For
npm-package2
:
"name": "@quickstart/npm-package2"
- For
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" }
Upload a package to the repository
Refresh the access token for connecting to the repository.
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
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
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.