npm ci 在 GitHub Actions 中失败

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

我正在开发一个使用 GitHub Actions 安装 npm 包的管道,我收到此错误:

npm ERR! code EUSAGE
npm ERR! 
npm ERR! The `npm ci` command can only install with an existing package-lock.json or
npm ERR! npm-shrinkwrap.json with lockfileVersion >= 1. Run an install with npm@5 or
npm ERR! later to generate a package-lock.json file, then try again.
npm ERR! 
npm ERR! Clean install a project
npm ERR! 
npm ERR! Usage:
npm ERR! npm ci
npm ERR! 
npm ERR! Options:
npm ERR! [--no-audit] [--foreground-scripts] [--ignore-scripts]
npm ERR! [--script-shell <script-shell>]
npm ERR! 
npm ERR! aliases: clean-install, ic, install-clean, isntall-clean
npm ERR! 
npm ERR! Run "npm help ci" for more info

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/runner/.npm/_logs/2022-09-09T06_53_34_562Z-debug-0.log
Error: Process completed with exit code 1.

我的管道看起来像这样:

name: Veracode frontend Scan
on:
  workflow_dispatch:
    
jobs:
  veracode:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - run: npm ci

如果有人可以帮助我,我不确定我哪里出错了。

npm github-actions npm-install npm-ci
3个回答
4
投票

首先检查这是否与

actions/setup-node
问题 498PR 103

有关

npm-ci
命令确实提到:

使用

npm install
npm ci
之间的主要区别是:

  • 该项目必须具有现有的
    package-lock.json
    npm-shrinkwrap.json
  • 如果包锁中的依赖项与
    package.json
    中的依赖项不匹配,
    npm ci
    将错误退出,而不是更新包锁。
  • ...

确保:

  • 您的
    npm
    项目会创建一个
    package-lock.json
    (默认情况下应该如此)
  • 它不会被忽略 (
    .gitignore
    ),并且是您的存储库代码库的一部分。

正如此处所指出的

为 CI/CD 运行

npm install
从根本上来说是有缺陷的。
运行测试可能是可以接受的,但如果您依赖可重现结果,则不能也不得使用
npm install

问题是

npm install
将选择最新的版本,并且仍然与
package.json
中指定的 semver 范围匹配。
这通常在一段时间内效果很好,但如果您突然被补丁版本引入的损坏的依赖项所困扰,您可能会花费数小时试图找出您之前使用的确切版本。

如果没有

package-lock.json
,您的可追溯性为零,并且几乎没有机会再次复制之前的版本。
坦率地说,任何人都建议否则永远不必处理损坏的生产构建,因为 CI/CD 方面的开发依赖关系破裂了。


OP学习者评论中确认了路径问题:

问题是我一直在错误的目录中运行命令,一旦我切换了

package-lock.json
所在的位置,就没有问题了。

但是错误信息变成:

UPSTREAM ERROR 

Fix the upstream dependency conflict, or retry this command with 
--force, or --legacy-peer-deps to accept an incorrect (and potentially broken) 
dependency resolution

这个答案来看,

npm ci --force
是在这种情况下前进的一种方法。


0
投票

当我们删除现有的package-lock.json时可能会出现此问题 或者当我们更新节点版本并为此删除 现有的 package-lock.json。任何管道(Azure、 github 操作等)。

因此,您可以尝试在以下位置运行命令的解决方案:

install npm
npm ci

如果它没有修复,那么您正在更新运行的nodejs版本 以下命令:

sudo apt-get install -y nodejs # install whatever nodejs version you want

npm install -g nodemon@latest

0
投票

似乎您错过了先签出代码,只需添加 actions/checkout 步骤,例如:

name: Veracode frontend Scan
on:
  workflow_dispatch:
    
jobs:
  veracode:
    runs-on: ubuntu-latest
    steps:
    - actions/checkout@v3
    - uses: actions/setup-node@v3
      with:
        node-version: 16
    - run: npm ci
© www.soinside.com 2019 - 2024. All rights reserved.