Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

github actions can not find package.json

I'm trying to set-up some basic GitHub action to write comments on the PRs.

Action is published on github and looks like this:

action.yml file:

name: !!name!!
description: !!description!!
author: !!me!!
inputs:
  token:
    description: "Github token"
    required: true
runs:
  using: "node12"
  main: "index.js"

index.js file:

const core = require("@actions/core");
const { execSync } = require("child_process");
const { GitHub, context } = require("@actions/github");

const main = async () => {
  const repoName = context.repo.repo;
  const repoOwner = context.repo.owner;
  const token = core.getInput("token");
  const testCommand = "yarn test --watchAll=false";
  const prNumber = context.payload.number;

  const githubClient = new GitHub(token);

  const reportAsString = execSync(testCommand).toString();

  const commentBody = `<div><h2>Test report</h2>
    <p>
      <pre>${reportAsString}</pre>
    </p>
  </div>`;

  await githubClient.issues.createComment({
    repo: repoName,
    owner: repoOwner,
    body: commentBody,
    issue_number: prNumber,
  });
};

main().catch((err) => core.setFailed(err.message));

In the project, I've added action through github, like this

.github/workflows/main.yml file:

on: [push]

jobs:
  start_tests:
    runs-on: ubuntu-latest
    name: A test job
    steps:
      - name: !!name!!
        uses: !!link to my action on github with version!!
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

However, my PRs actions are failing and this is the reason:

 error Couldn't find a package.json file in "/home/runner/work/!!project_name!!/!!project_name!!"
##[error]Command failed: yarn test --watchAll=false
error Couldn't find a package.json file in "/home/runner/work/!!project_name!!/!!project_name!!"

So, anyone has an idea what am I doing wrong here? I tried to google the solution but erm... unsuccessfully :(

Thanks for your time!

like image 712
Uros Avatar asked Jun 22 '26 13:06

Uros


1 Answers

package.json file has to be present to run yarn test command. As we can see in error there is no such file just because there is no project folder at all. It means that job doesn't know anything about project to work with. So, you have to do one of the following changes:

in case your action is published to marketplace

on: [push]

jobs:
  start_tests:
    runs-on: ubuntu-latest
    name: A test job
    steps:
      - uses: actions/[email protected]
      - name: !!name!!
        uses: !!link to my action on github with version!!
        with:
          token: ${{ secrets.GITHUB_TOKEN }}

in case your action is not published yet or you just want to run it "locally"

on: [push]

jobs:
  start_tests:
    runs-on: ubuntu-latest
    name: A test job
    steps:
      - name: !!name!!
        uses: ./
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
like image 183
fabasoad Avatar answered Jun 25 '26 17:06

fabasoad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!