6

I'm having an issue with installing an NPM package from GCP.

I was able to upload the package to the artifact registry of GCP by doing the following steps:

  1. Login to my google account (gcloud auth application-default login)

  2. Run

    gcloud artifacts print-settings npm \ --project=[my-project]\ --repository=[my-repo] \ --location=us-east1 \ --scope=@[my-scope]

  3. Pasting the output of the previous step in the .npmrc file located in the root of the project.

  4. Refreshing the access token to GCP (npx google-artifactregistry-auth ./.npmrc)

  5. Run yarn publish

My .npmrc file looks like this:

@[my-scope]:registry=https://us-east1-npm.pkg.dev/[my-project]/[my-repo]/
//us-east1-npm.pkg.dev/[my-project]/[my-repo]/:_authToken="[auth-token]"
//us-east1-npm.pkg.dev/[my-project]/[my-repo]/:always-auth=true

However, when I try to install the package on another project by:

  1. Executing steps 1-4 mentioned above
  2. Run yarn add @[my-scope]/[my-package]

I get an 404 error. Looks like yarn is looking for the package in the default registry:

error An unexpected error occurred: "https://registry.yarnpkg.com/@[my-scope]/@[my-pacakge]/-/@[my-scope]/[my-package]-0.0.1.tgz: Request failed \"404 Not Found\"".

I simply followed the steps mentioned in the installation instructions in GCP but somehow it's not working.

I encountered a similar issue in this post: Can't install a scoped package I published to a npm registry in GCP but this not the exact error I get.

I would appreciate any help regarding this issue.

Thanks in advance!

2 Answers2

5

I just had this problem for a couple of days and the solution is simple, DO NOT USE YARN when publishing. That's it.

I don't know which part of yarn causes this but basically it ignores .npmrc resulting in the tarball to point to the wrong repository, you can check it if you run yarn info. So when publishing to GCP artifact registry one should use npm publish instead.

ITfromPH
  • 86
  • 1
  • 4
  • Not necessarily. You can use yarn if you write a `.yarnrc` file: https://stackoverflow.com/questions/58882143/yarn-with-npmrc-and-authentication – jackdbd Jul 16 '22 at 07:51
  • 1
    Yes but in this specific case the auth helper that google wrote only supports npmrc, at least on my tests hence the solution that I recommended. – ITfromPH Jul 17 '22 at 08:04
0

In both setting up authentication for npm and Managing Node.js packages, Obtaining an access token section the command used is

npx google-artifactregistry-auth

In the same section there is a note that explains how to add flags if you need to change the path of the .npmrc file.

Note: If you need to store your repository settings and credentials in .npmrc files other than the defaults, you can run the credential helper with additional flags.

--repo-config is the .npmrc file with your repository settings. If you don't specify this flag, the default location is the current directory.

--credential-config is the path to the .npmrc file where you want to write the access token. The default is your user .npmrc file.

Instead of:

npx google-artifactregistry-auth ./.npmrc

It could be written as

npx google-artifactregistry-auth --repo-config=pathto/.npmrc --credential-config=pathto/.npmrc 

If you are not sure where your file is you can run npm config ls -l | grep config as explained here

Also check you are specifying the correct .npmrc path if it is different than the default registry as shown in Configuring npm and confirm you are trying to install a package from the Node.js package repository with the correct scope, package, tag or version to be completely explicit.

Alex
  • 778
  • 1
  • 15
  • 1
    I followed again the steps in the links you sent me. I created a key file for the service account and created an environment variable with the path to it. After running `npx google-artifactregistry-auth` without the path as you suggested, the second line in the .npmrc file disappears. When I do run it with the path to .npmrc, it simply fills the "authToken" value, which is why I assumed that this is the correct way to run this command. – Ori Netanel Ben-Zaken May 08 '22 at 07:38
  • 1
    In addition, what do you mean by ” using an .npmrc per account and not the same configuration file duplicated in each”. Besides the token, all the other content should be identical in the project where we publish/install the package. – Ori Netanel Ben-Zaken May 08 '22 at 07:39
  • I've edited my question to try to be more specific. Could you please check it out? – Alex May 13 '22 at 00:24