简体   繁体   English

NPM 坚持使用 CodeArtifact,甚至在公共注册表上

[英]NPM insists on CodeArtifact, even on public registry

I'm using npmrc to setup my corporate (using CodeArtifact) and public (using registry.npmjs.org ) NPM registries.我正在使用npmrc设置我的公司(使用 CodeArtifact)和公共(使用registry.npmjs.org )NPM 注册表。

The problem is that, even with the public profile selected, I keep getting an error that the authentication to CodeArtifact is missing.问题是,即使选择了公共配置文件,我仍然收到一个错误,即缺少对 CodeArtifact 的身份验证。 The only way to get rid of this is authenticating on CodeArtifact, which causes the auth token to be set to both profiles.摆脱这种情况的唯一方法是在 CodeArtifact 上进行身份验证,这会导致将身份验证令牌设置为两个配置文件。 After that, regardless the profile selected, both uses CodeArtifact as a registry.之后,无论选择哪个配置文件,都使用 CodeArtifact 作为注册表。

I tried extreme measures and deleted all my profiles and create a single one set to registry.npmjs.org .我尝试了极端措施并删除了我的所有配置文件并创建了一个集合到registry.npmjs.org Still, NPM (and Yarn) complain about missing auth to CodeArtifact.尽管如此,NPM(和 Yarn)仍然抱怨缺少对 CodeArtifact 的身份验证。

Any ideas?有任何想法吗?

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:假设您使用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

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 ).这些命令是aws codeartifact login --tool npm --repository my-repo --domain my-domain更多信息)的解构,不同之处在于不是在您的.npmrc文件中设置通用registry (用于设置配置为您的 npm)将设置一个范围注册表更多信息)。 In this way you will be able to have you fetch your packages from the sources you want.通过这种方式,您将能够从您想要的来源获取您的包。 In your case have access to registry.npmjs.org without being asked for auth.在您的情况下,无需进行身份验证即可访问registry.npmjs.org

Here is an enhanced solution as used in my bash script:这是我的 bash 脚本中使用的增强解决方案:

#!/bin/bash
ACCOUNT_NO=$(aws sts get-caller-identity --query "Account" --output text)
COMPANY=my_company
ENDPOINT=$(aws codeartifact get-repository-endpoint --domain $COMPANY --domain-owner $ACCOUNT_NO --repository $COMPANY --format npm --output text)
TOKEN=$(aws codeartifact get-authorization-token --domain $COMPANY --domain-owner $ACCOUNT_NO --duration-seconds 43200 --query authorizationToken --output text)
npm config set @$COMPANY:registry $ENDPOINT
npm config set ${ENDPOINT:6}:_authToken=$TOKEN
npm config set ${ENDPOINT:6}:always-auth=true

I viewed the output of npm config list and it looks as if it was set up manually.我查看了npm config list的 output ,它看起来好像是手动设置的。

I tested and was able to download the private test (from CodeArtifact) and the totally public cypress :我测试并能够下载私人test (来自 CodeArtifact)和完全公开的cypress

npm install @my_company/test
npm install @my_company/test@0.0.1
npm install cypress

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM