当存在yarn.lock时安装私有npmjs包失败

问题描述 投票:0回答:1

我有一个项目依赖于一些发布到私人npmjs帐户的包。例如,

@myaccount/first-package

我有一个项目级别

.npmrc
文件,具有以下配置,

@myaccount:registry=https://registry.npmjs.org/
//registry.npmjs.org/:_authToken=npm_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
always-auth=true

这里使用的Auth token是从npmjs web生成的,它是一个经典的token。我尝试过只读令牌和发布(读取+发布)令牌。

Yarn 配置似乎也在加载正确的配置。以下是

yarn config list
,

的输出
yarn config v1.22.19
info yarn config
{
  'version-tag-prefix': 'v',
  'version-git-tag': true,
  'version-commit-hooks': true,
  'version-git-sign': false,
  'version-git-message': 'v%s',
  'init-version': '1.0.0',
  'init-license': 'MIT',
  'save-prefix': '^',
  'bin-links': true,
  'ignore-scripts': false,
  'ignore-optional': false,
  registry: 'https://registry.yarnpkg.com',
  'strict-ssl': true,
  'user-agent': 'yarn/1.22.19 npm/? node/v18.17.1 linux x64',
  lastUpdateCheck: 1693395311360
}
info npm config
{
  '@myaccount:registry': 'https://registry.npmjs.org/',
  '//registry.npmjs.org/:_authToken': 'npm_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  'always-auth': true
}

通过此设置,当我第一次运行

yarn install
时,
yarn.lock
没有
@myaccount/first-package
的条目或
yarn.lock
文件不存在,它会正确地将包安装到 node_modules 并添加以下条目到
yarn.lock

"@myaccount/first-package@^0.2.36":
  version "0.2.36"
  resolved "https://registry.yarnpkg.com/@myaccount/first-package/-/first-package-0.2.36.tgz#340c2c976497bad20d7552e52608ac8d5355e2ab"
  integrity sha512-45dk+EpASIHs8g+bC+DPD/VeZIbDbL74xJ4viIj+K9Sx0Qv3M8I/EoXvTG1zHxmz/7Itt5AmpwH7/vxlop26IQ==

但是,一旦我删除node_modules并重新运行

yarn install
或其他人(开发人员和cicd docker客户端)检查具有新
yarn.lock
的代码并运行
yarn install
,他们就会收到404错误,如下所示,

error An unexpected error occurred: "https://registry.yarnpkg.com/@myaccount/first-package/-/first-package-0.2.36.tgz: Request failed \"404 Not Found\"".

但是,一旦他们删除

yarn.lock
并运行
yarn install
,他们就可以毫无问题地安装软件包。

安装命令 (

yarn install --verbose
) 的详细输出如下所示,

成功场景:

verbose 3.066711833 Performing "GET" request to "https://registry.npmjs.org/@myaccount%2ffirst-package".
verbose 3.809710167 Request "https://registry.npmjs.org/@myaccount%2ffirst-package" finished with status code 200.

失败场景:

verbose 1.960352751 Performing "GET" request to "https://registry.yarnpkg.com/@myaccount/first-package/-/first-package-0.2.36.tgz".
verbose 5.081442544 Error: https://registry.yarnpkg.com/@myaccount/first-package/-/first-package-0.2.36.tgz: Request failed "404 Not Found"

此行为的原因是什么以及如何解决?

我正在使用,

Node.js v18.17.1
npm     v8.19.2
yarn    v1.22.19

更多注释:

  • 使用带有身份验证承载令牌的 Postman 请求相同的失败 URL,返回 200。
  • 使用npm代替yarn不会导致这个问题。但是,我无法更改包管理器。
  • .npmrc
    文件为全局 (
    ~/.npmrc
    ) 时,问题保持不变。
  • 删除
    yarn.lock
    不是一个选项,因为我高度依赖它的 CICD 管道。
  • 在这种情况下,我手动将包添加到
    package.json
    ,然后由于项目中的一些其他内务工作而运行
    yarn install
node.js npm yarnpkg
1个回答
0
投票

您可以通过以下方式解决问题:

$ yarn config set registry https://registry.npmjs.org
$ rm yarn.lock
$ yarn

上述步骤将重新生成您的

yarn.lock
文件

© www.soinside.com 2019 - 2024. All rights reserved.