This repository contains tools to simplify the process of working with npm packages using Artifact Registry.
The Artifact Registry google-artifactregistry-auth module is an npm package which allows you to configure npm to interact with npm private repositories in Artifact Registry.
For more details, see https://cloud.google.com/artifact-registry/docs/nodejs/authentication
The module automatically searches for credentials from the environment and authenticates to Artifact Registry. It looks for credentials in the following order:
- Google Application Default Credentials.
- The current active account logged in via
gcloud auth login. - If neither of them exist, an error occurs.
NOTE: This module would update credentials for all Artifact Registry repositories. It would not be suitable if you use multiple account credentials in npmrc file.
To use the module:
-
Log in
Option 1: log in as a service account:
(1). Using a JSON file that contains a service account key:
`$ export GOOGLE_APPLICATION_CREDENTIALS=[path/to/key.json]`(2). Or using gcloud:
`$ gcloud auth application-default login`Option 2: log in as an end user via gcloud:
$ gcloud auth login -
Add settings to connect to the repository to .npmrc. Use the output from the following command:
$ gcloud artifacts print-settings npmregistry=https://LOCATION-npm.pkg.dev/PROJECT_ID/REPOSITORY_ID/ //LOCATION-npm.pkg.dev/PROJECT_ID/REPOSITORY_ID/:always-auth=trueWhere
PROJECT_ID is the ID of the project.
REPOSITORY_ID is the ID of the repository.
LOCATION is the location of the repository.
-
Use one of these below options to run the script
-
Run the module outside of the directory containing the target npmrc file
$ npx google-artifactregistry-auth --repo-config=[./.npmrc] --credential-config=[~/.npmrc] -
Include the command in the scripts in package.json
"scripts": { "artifactregistry-login": "npx google-artifactregistry-auth --repo-config=[./.npmrc] --credential-config=[~/.npmrc]", }Where:
--repo-configis the.npmrcfile with your repository settings. If you don't specify this flag, the default location is the current directory.--credential-configis the path to the.npmrcfile where you want to write the access token. The default is your user.npmrcfile.
And then run the script
$ npm run artifactregistry-login -
npxshould come withnpm5.2+. Ifnpxis not available:Install the module from npmjs.com as a dev dependency and include the command in the script
$ npm install google-artifactregistry-auth --save-dev"scripts": { "artifactregistry-login": "./node_modules/.bin/artifactregistry-auth --repo-config=[./.npmrc] --credential-config=[~/.npmrc]", }Run the script
$ npm run artifactregistry-login
-
If you are running the script from an isolated environment, meaning there is no egress to public npm registry, the npx command will fail because it cannot download the package from npmjs.com or from private Artifact Registry yet. In this case, pre-install the package in the isolated environment and run the javascript directly to avoid the npx command reaching out to the internet.
-
Pre-Install the package in the isolated environment (e.g. Cloud Build)
$ npm install -g google-artifactregistry-auth -
Run the script directly.
$ node $(npm root -g)/google-artifactregistry-auth/src/main.js --repo-config=[./.npmrc] --credential-config=[~/.npmrc]