はじめに
CI/CD の学習を始めたので、GitHub Actions を使って React アプリの build に挑戦しました。
最初はなかなかうまくいかず、package.json が見つからないエラーなどでつまずいたので、原因と対応方法をメモとして残します。
問題
下記コードでmainにpushしたところ、エラーが発生。
name: Learn GitHub Actions
on:
push:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- run: npm run build
Run npm run build
npm error code ENOENT
npm error syscall open
npm error path /home/runner/work/original-react0524/original-react0524/package.json
npm error errno -2
npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open '/home/runner/work/original-react0524/original-react0524/package.json'
npm error enoent This is related to npm not being able to find a file.
npm error enoent
npm error A complete log of this run can be found in: /home/runner/.npm/_logs/2026-06-11T21_25_51_612Z-debug-0.log
Error: Process completed with exit code 254.
原因
npm error enoent Could not read package.json: Error: ENOENT: no such file or directory, open
訳:npm が package.json を開こうとしたけど、その場所にファイルがなかったため失敗しました
なるほど。
解決
name: Learn GitHub Actions
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
# コードを GitHub Actions の実行環境に持ってくる
- name: Checkout code
uses: actions/checkout@v4
# build するための Node.js を用意する
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'npm'
# package-lock.json をもとに依存関係をインストールする
- name: Install dependencies
run: npm ci
# build を実行する
- name: Build project
run: npm run build
GitHub Actions の実行環境は、最初はほぼ空っぽ。
そのため、まず actions/checkout を使ってリポジトリのコードを取得する必要がある。
今回のエラーは、actions/checkout を設定していなかったので、GitHub Actions の実行環境に package.json がなくて、npm run build が失敗してたみたい。
色々抜けてた。
React アプリを build するには Node.js と依存パッケージも必要なので、actions/setup-node で Node.js を用意して、npm ci で依存関係をインストールしてから npm run build を実行で無事に解決しました!
参考
ありがとうございました!