7

I have some different projects in my local machine. Some of then uses AWS Codeartifact to download private dependencies in AWS Codeartifact, and others do not use. The projects that uses AWS Codeartifact manage their dependencies with Yarn, and my projects that do not uses AWS Codeartifact manage their dependencies with NPM.

When i run a simple command in NPM like:

npm install nanoid

The npm tries to connect to my AWS Codeartifact and it gives an error:

Unable to authenticate, need: Bearer realm="domain-npm-repo/npm-repo", Basic realm="domain-npm-repo/npm-repo"

How can i configure my machine to use AWS Codearticaft only to the projects i want?

Other Configurations:

My machine is a Windows 10, and i have aws-sdk installed globally with my credentials.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Matheus Carvalho
  • 415
  • 7
  • 13

3 Answers3

19

I solved it running the command:

npm config set registry https://registry.npmjs.org/

It set my npm registry back to the default value and uses the npm registry instead of my aws codeartifact registry.

Matheus Carvalho
  • 415
  • 7
  • 13
6

Assuming you are using aws codeartifact login --tool npm --repository my-repo --domain my-domain to login into aws you should use a more granular approach use the following commands:

# get endpoint 
endpoint = aws codeartifact get-repository-endpoint --domain my_domain --domain-owner 111122223333 --repository my_repo --format npm

# set a scoped registry
npm config set registry endpoint --scope=@my-package <- this is what you want

# get token
token = aws codeartifact get-authorization-token --domain my_domain --domain-owner 111122223333 --repository my_repo

# set token
npm config set //my_domain-111122223333.d.codeartifact.region.amazonaws.com/npm/my_repo/:_authToken=token

# always truth
npm config set //my_domain-111122223333.d.codeartifact.region.amazonaws.com/npm/my_repo/:always-auth=true

These commands are a deconstruction of aws codeartifact login --tool npm --repository my-repo --domain my-domain (more info), with the difference that instead of setting a general registry at your .npmrc file (used to set configurations for your npm) will set a scoped registry (more info). In this way you will be able to have you fetch your packages from the sources you want.

sebassebas1313
  • 363
  • 2
  • 8
  • 1
    The documentation AWS links: https://docs.aws.amazon.com/cli/latest/reference/codeartifact/get-repository-endpoint.html https://docs.aws.amazon.com/cli/latest/reference/codeartifact/get-authorization-token.html – Anton Krug Apr 10 '21 at 14:21
  • Thanks for the answer! It helped a lot and indeed is more granular. Thanks! – Matheus Carvalho Apr 22 '21 at 18:24
  • Note that the login command also supports the `--namespace` flag (which in effect scopes the registry url in .npmrc). – Jason L Nov 01 '21 at 20:54
0

Should anybody land on this page in the future. None of the above solutions worked for me. What did work is adding a trailing slash in the publishConfig.registry property of my package.json.

Spangaer
  • 391
  • 3
  • 3