Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

At wits end; 'gcloud functions deploy' fails to authenticate with npm Artifact Registry

dripus
New Member

I'm trying to set up a NodeJS Google Run function (2nd generation), but deployment fails when I include my private npm registry hosted on GCP's Artifact Registry. If I deploy my function without this npm @scope dependency, everything works.

Locally I am able to authenticate with the registry, I see .npmrc updated with my token. I am able to successfully run "npm install @scope/mylib"
I tried deploying with my auth token hard-coded in .npmrc, but it always fails on E401 (I guess it's because the token belongs to my user and not the build-time user)

I verified IAM permissions (used both default build user & created a user and gave it needed roles). Both build-time users have Artifact Reader role on the specific repository they are trying to read from 

It seems like when I run the command the build doesn't authenticate with the Registry and I don't know what I should do. I tried to add "artifactregistry-login" script to my package.json (following this guide) - also no help.

Help?

---------------------------------------

gcloud functions deploy my-func
  --gen2
  --runtime=nodejs20
  --trigger-http
  --region=northamerica-northeast1
  --source=.
  --entry-point=my-func-entry
  --run-service-account=my-func-service-account@myorg.iam.gserviceaccount.com
  --build-service-account=<I tried with specific account or the default one, it doesn't help>

---------------------------------------

PS C:\IdeaProjects\myFunc> gcloud artifacts repositories get-iam-policy my-func-registry --location=northamerica-northeast1 --project=myorg 
bindings:
- members:
- serviceAccount:my-func-service-account@myorg.iam.gserviceaccount.com
role: roles/artifactregistry.reader
- members:
- serviceAccount:my-func-service-account@myorg.iam.gserviceaccount.com
role: roles/artifactregistry.writer
etag: BwY4VdFFxd0=
version: 1

---------------------------------------

...
...
Image northamerica-northeast1-docker.pkg.dev/myorg/gcf-artifacts/my--org__northamerica--northeast1__myfunc/cache:latest exists
Checking if image northamerica-northeast1-docker.pkg.dev/myorg/gcf-artifacts/my--org__northamerica--northeast1__myfunc/cache:latest is valid
Image northamerica-northeast1-docker.pkg.dev/myorg/gcf-artifacts/my--org__northamerica--northeast1__myfunc/cache:latest is valid
Finished Step #1 - "pre-buildpack"
Starting Step #2 - "build"
Already have image (with digest): northamerica-northeast1-docker.pkg.dev/serverless-runtimes/google-22-full/builder/nodejs:nodejs_20250615_RC00
===> ANALYZING
Restoring data for SBOM from previous image
===> DETECTING
target distro name/version labels not found, reading /etc/os-release file
google.nodejs.runtime 1.0.0
google.utils.archive-source 0.0.1
google.nodejs.npm 1.1.0
google.nodejs.functions-framework 0.9.4
google.config.entrypoint 0.9.0
google.utils.label-image 0.0.2
===> RESTORING
Restoring metadata for "google.nodejs.runtime:node" from cache
Restoring metadata for "google.utils.archive-source:src" from app image
Restoring metadata for "google.nodejs.npm:npm_modules" from cache
Restoring metadata for "google.nodejs.functions-framework:functions-framework" from app image
Restoring data for "google.nodejs.runtime:node" from cache
Restoring data for "google.nodejs.npm:npm_modules" from cache
Restoring data for "google.nodejs.functions-framework:functions-framework" from cache
===> BUILDING
target distro name/version labels not found, reading /etc/os-release file
=== Node.js - Runtime (google.nodejs.runtime@1.0.0) ===
Using runtime version from GOOGLE_RUNTIME_VERSION: 20.19.2
***** CACHE HIT: "nodejs"
Node.js v20.19.2 cache hit, skipping installation.
=== Utils - Archive Source (google.utils.archive-source@0.0.1) ===
Adding image label google.source-archive: /workspace/.googlebuild/source-code.tar.gz
=== Node.js - Npm (google.nodejs.npm@1.1.0) ===
***** CACHE MISS: "npm_modules"
Installing application dependencies.
--------------------------------------------------------------------------------
Running "npm ci --quiet --no-fund --no-audit (NODE_ENV=production)"
npm warn deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated glob@6.0.4: Glob versions prior to v9 are no longer supported
npm error code E401
npm error Incorrect or missing password.
npm error If you were trying to login, change your password, create an
npm error authentication token or enable two-factor authentication then
npm error that means you likely typed your password in incorrectly.
npm error Please try again, or recover your password at:
npm error https://www.npmjs.com/forgot
npm error
npm error If you were doing some other operation then your saved credentials are
npm error probably out of date. To correct this please try logging in again with:
npm error npm login
npm error A complete log of this run can be found in: /www-data-home/.npm/_logs/2025-06-24T20_26_37_556Z-debug-0.log
Done "npm ci --quiet --no-fund --no-audit (NODE_ENV=production)" (3.467511655s)
--------------------------------------------------------------------------------
failed to build: (error ID: 7fa33aaa):
npm warn deprecated rimraf@2.4.5: Rimraf versions prior to v4 are no longer supported
npm warn deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated glob@6.0.4: Glob versions prior to v9 are no longer supported
npm error code E401
npm error Incorrect or missing password.
npm error If you were trying to login, change your password, create an
npm error authentication token or enable two-factor authentication then
npm error that means you likely typed your password in incorrectly.
npm error Please try again, or recover your password at:
npm error https://www.npmjs.com/forgot
npm error
npm error If you were doing some other operation then your saved credentials are
npm error probably out of date. To correct this please try logging in again with:
npm error npm login
npm error A complete log of this run can be found in: /www-data-home/.npm/_logs/2025-06-24T20_26_37_556Z-debug-0.log
ERROR: failed to build: exit status 1
Finished Step #2 - "build"
ERROR
ERROR: build step 2 "northamerica-northeast1-docker.pkg.dev/serverless-runtimes/google-22-full/builder/nodejs:nodejs_20250615_RC00" failed: step exited with non-zero status: 51

 

0 1 45
1 REPLY 1

dripus
New Member

Solved it myself (well, Gemini helped eventually);

I added this line at the very end of .gcloudignore file:

!.npmrc

It guarantees that .npmrc file is included in the zipped source sent to buildpack when running the gcloud command mentioned above. I think for some reason this file wasn't packed, and this addition ensured it'll be there.

Hope this helps someone