【问题标题】:npm publish not working on AWS CodeArtifactnpm publish 在 AWS CodeArtifact 上不起作用
【发布时间】:2021-05-17 21:07:19
【问题描述】:

我正在尝试使用 npm publish 将 javascript 库发布到托管在 AWS Codeartifact 上的私有 NPM 存储库。

当我尝试在本地计算机上运行 npm publish 时,它可以正常工作。但是,当我尝试在 Jenkins 上运行它时,看起来该包正试图发布到 NPM 的包存储库而不是 AWS codeartifact,并且我收到以下错误:

 > @company/package@0.2.2 prepare .
 > npm run co:login
 
 
 > @company/package@0.2.2 co:login /usr/src/components
 > aws codeartifact login --tool npm --domain <DOMAIN> --repository <REPOSITORY>
 
 Successfully configured npm to use AWS CodeArtifact repository <AWS_CODEARTIFACT_URL>
 Login expires in 12 hours at 2020-12-08 11:15:37+00:00
 npm notice 
 npm notice package: @company/package@0.2.2
 npm notice === Tarball Contents === 
 npm notice 2.9kB package.json   
 npm notice 1.6kB README.md      
 npm notice 268B  dist/index.d.ts
 npm notice === Tarball Details === 
 npm notice name:          @company/package              
 npm notice version:       0.2.2                                   
 npm notice package size:  2.1 kB                                  
 npm notice unpacked size: 4.8 kB                                  
 npm notice shasum:        <SHASUM>
 npm notice integrity:     <INTEGRITY>
 npm notice total files:   3                                       
 npm notice 
 npm ERR! code E404
 npm ERR! 404 Not Found - PUT https://registry.npmjs.org/@company%2fpackage - Not found
 npm ERR! 404 
 npm ERR! 404  '@company/package@0.2.3' is not in the npm registry.
 npm ERR! 404 You should bug the author to publish it (or use the name yourself!)
 npm ERR! 404 
 npm ERR! 404 Note that you can also install from a
 npm ERR! 404 tarball, folder, http url, or git url.
 
 npm ERR! A complete log of this run can be found in:
 npm ERR!     /root/.npm/_logs/2020-12-08T00_20_19_949Z-debug.log

如果我理解AWS codeartifact documentation,当我们运行aws codeartifact login 命令时,它应该在.npmrc 中设置正确的设置。我仍然不明白为什么它试图推送到 npm 注册表而不是 AWS。

这是我的 package.json:

{
  "name": "@company/package",
  "version": <VERSION>,
  "main": "dist/index.js",
  "module": "dist/index.js",
  "types": "dist/index.d.ts",
  "files": [
    "dist"
  ],
  "dependencies": {...},
  "scripts": {
    "co:login": "aws codeartifact login --tool npm --domain <DOMAIN> --repository <REPOSITORY>",
    "prepare": "npm run co:login"
  },
  "eslintConfig": {...},
  "browserslist": {...},
  "peerDependencies": {...},
  "devDependencies": {...}
}

更新

我将链接添加到我的 CodeArtificat 使用

npm set registry <LINK_TO_CODE_ARTIFACT>

当我在本地执行发布脚本时,脚本首次运行时出现此错误:

Unable to authenticate, need: Bearer realm="company/package", Basic realm="company/package"

该错误在随后的运行中消失。但是,当我在 Jenkins 管道上尝试相同的操作时,我不断收到此错误。不知道这意味着什么。我在 Azure 上看到了与此错误相关的线程,但在 AWS CodeArtifact 上找不到任何内容。

【问题讨论】:

    标签: node.js amazon-web-services npm npm-publish aws-codeartifact


    【解决方案1】:

    问题是我在这个项目中使用了两个不同的包管理器yarnnpm 来完成几个任务。代码是使用yarn 构建的,但发布是在npm 上进行的。为了统一,即在我们所有的任务中使用yarn,我尝试了yarn publish,它神奇地起作用了。

    故事的寓意:与你的包管理器保持一致。

    【讨论】:

      【解决方案2】:

      我有一个非常相似的问题,使用 CodeBuild 和 CodeArtifact 发布 npm 包。

      使用prepare 步骤和aws codeartifact login 命令的推荐方法对我不起作用。我认为这可能是由于Ubuntunpm 不允许实际访问login 命令写入的.npmrc 文件造成的。

      最后,我使用来自https://docs.aws.amazon.com/codeartifact/latest/ug/npm-auth.html 的建议在npm publish 之前将以下额外的shell 命令添加到我的脚本中:

        npm set registry https://<endpoint name>.d.codeartifact.eu-west-2.amazonaws.com/npm/<repo name>/
        npm config set //<endpoint name>.d.codeartifact.eu-west-2.amazonaws.com/npm/<repo name>/:always-auth=true
        npm config set //<endpoint name>.d.codeartifact.eu-west-2.amazonaws.com/npm/<repo name>/:_authToken=$(aws codeartifact get-authorization-token --domain <domain> | jq -r .authorizationToken)
        npm publish --unsafe-perm
      
      

      注意,端点名称和域名将取决于您自己的设置。

      另请注意,我已使用 jq 提取身份验证令牌 - 这是 CodeBuild 服务器上的标准配置。

      还请注意,此示例位于 eu-west-2,但如果您更改它,则可能位于任何区域。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-03
        • 1970-01-01
        • 2023-01-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多