NPM 始终使用 AWS Codeartifact

问题描述

我的本​​地机器上有一些不同的项目。其中一些使用 AWS Codeartifact 下载 AWS Codeartifact 中的私有依赖项,而另一些则不使用。使用 AWS Codeartifact 的项目使用 Yarn 管理它们的依赖项,而我不使用 AWS Codeartifact 的项目使用 NPM 管理它们的依赖项。

当我在 NPM 中运行一个简单的命令时:

npm install nanoid

npm 尝试连接到我的 AWS Codeartifact 并出现错误

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

如何将我的机器配置为仅将 AWS Codearticaft 用于我想要的项目?

其他配置:

我的机器是 Windows 10,我使用我的凭据全局安装了 aws-sdk。

解决方法

假设您使用 aws codeartifact login --tool npm --repository my-repo --domain my-domain 登录 aws,您应该使用更精细的方法,使用以下命令:

# 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

这些命令是对 aws codeartifact login --tool npm --repository my-repo --domain my-domain (more info) 的解构,区别在于不是在您的 registry 文件中设置通用的 .npmrc(用于设置您的 npm) 将设置一个范围的注册表 (more info)。 通过这种方式,您将能够从您想要的来源获取您的包裹。

,

我运行命令解决了它:

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

它将我的 npm 注册表设置回默认值,并使用 npm 注册表而不是我的 aws codeartifact 注册表。