NPM always uses AWS Codeartifact
Asked Answered
B

5

10

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.

Berwickupontweed answered 6/2, 2021 at 14:59 Comment(0)
B
26

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.

Berwickupontweed answered 6/2, 2021 at 15:3 Comment(0)
C
7

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.

Chasitychasm answered 10/4, 2021 at 13:42 Comment(3)
The documentation AWS links: docs.aws.amazon.com/cli/latest/reference/codeartifact/… docs.aws.amazon.com/cli/latest/reference/codeartifact/…Anatole
Thanks for the answer! It helped a lot and indeed is more granular. Thanks!Berwickupontweed
Note that the login command also supports the --namespace flag (which in effect scopes the registry url in .npmrc).Magna
A
1

As a dummy fix - you can delete everything from your .npmrc file, when you don't want to use codeartifact. But the file will be updated every time you run your codeartifact commands, so you will have to clean this file every time you want to run the non-codeartifact projects.

Acculturation answered 19/9, 2023 at 14:34 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Na
L
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.

Lammergeier answered 23/8, 2022 at 8:49 Comment(0)
I
0

if you are using yarn, delete both: .npmrc and .yarnrc, and re-install dependencies

Innermost answered 27/8 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.