Npm install 在 github 操作中失败

Posted

技术标签:

【中文标题】Npm install 在 github 操作中失败【英文标题】:Npm install falied in github action 【发布时间】:2021-10-06 23:41:28 【问题描述】:

我尝试创建一个 github 操作,使用 vue 和 vue-cli-plugin-electron-builder 构建我的电子应用程序,但我无法通过 npm 安装依赖项

我的文件夹树是这样的:

|   .gitignore
|   babel.config.js
|   LICENSE
|   package-lock.json
|   package.json
|   README.md
|   vue.config.js
|   yarn.lock
|
+---public
|       favicon.ico
|       index.html
|       test.jpg
|
\---src
    |   App.vue
    |   background.js
    |   main.js
    |   preload.js
    |
    +---assets
    |       ...
    |
    \---components
            ...

当我运行 CI 时,我发现了一个错误

  npm ci
  shell: C:\Program Files\PowerShell\7\pwsh.EXE -command ". '0'"
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! A complete log of this run can be found in:
npm ERR!     C:\npm\cache\_logs\2021-08-01T04_59_20_385Z-debug.log
Error: Process completed with exit code 1.

这是我的空白.yml:

# This is a basic workflow to help you get started with Actions

name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the main branch
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    name: Build
    runs-on: windows-latest
    steps:
    - name: Install Node.js, NPM and Yarn
      uses: actions/setup-node@v1
      with:
          node-version: 16
    - name: Install Dependencies
      run: |
            npm ci
    - name: Electron Build
      run: |
            npm run electron:build --windows nsis --x64 --ia32

我尝试使用关键字“npm install failed in github action”在 bing 上进行搜索,但没有发现任何帮助。 另外,我尝试使用“npm install”而不是“npm ci”,但它显示了相同的错误消息,即找不到 package.json 'npm install' 的错误信息如下:

Run npm install
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path D:\a\example\example/package.json
npm ERR! errno -4058
npm ERR! enoent ENOENT: no such file or directory, open 'D:\a\example\example\package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent 

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\npm\cache\_logs\2021-08-01T05_12_56_060Z-debug.log
Error: Process completed with exit code 1.

我该如何解决?

英语不是我的母语;请原谅打字错误。

【问题讨论】:

【参考方案1】:

您没有检查您的代码并直接运行NPM CI。 下面的代码应该为您完成这项工作

name: CI
on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    name: Build
    runs-on: windows-latest
    steps:
    - name: Checkout Code
      uses: actions/checkout@v2
    - name: Install Node.js, NPM and Yarn
      uses: actions/setup-node@v1
      with:
          node-version: 16
    - name: Install Dependencies
      run: |
            npm ci
    - name: Electron Build
      run: |
            npm run electron:build --windows nsis --x64 --ia32

【讨论】:

以上是关于Npm install 在 github 操作中失败的主要内容,如果未能解决你的问题,请参考以下文章

`npm install` 以“Killed”结尾

npm install

如何在没有 npm install 的情况下从 github 运行 react 项目

为啥 yarn install checkout github 存储库依赖项但 npm install 没有?

私有 github repo 作为依赖在 npm install 上是无关的

npm install 不在 GitHub 上安装最新版本