GitHub Actions actions/github-script "require is not defined" (ESM)
actions/github-script evaluates the script body as an async function with require and the octokit/context globals available. Writing import statements, or treating the body as an ESM module, breaks the CommonJS contract and require can appear undefined.
What this error means
A github-script step throws ReferenceError: require is not defined, or a SyntaxError about import, when the inline script uses ESM syntax.
github-actions
ReferenceError: require is not defined
at eval (eval at callAsyncFunction ...)Common causes
ESM import used instead of require
import x from y is not valid in the CommonJS context the action evaluates; use require or the provided globals.
External .mjs script
Pointing the action at an ESM module file can break the require-based bindings it injects.
How to fix it
Use CommonJS and provided globals
- Replace import with require inside the script.
- Use the injected github, context, and core globals directly.
- Keep external scripts as CommonJS (.cjs/.js) if you split them out.
.github/workflows/ci.yml
- uses: actions/github-script@v7
with:
script: |
const { data } = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
});
core.info(`found ${data.length} issues`);How to prevent it
- Treat the github-script body as CommonJS, not an ESM module.
- Lean on the injected github/context/core globals instead of imports.
Related guides
actions/github-script: Run JavaScript Against the GitHub APIReference for actions/github-script: run inline JavaScript with a pre-authenticated Octokit client to comment…
GitHub Actions actions/stale "API rate limit exceeded"Fix actions/stale "API rate limit exceeded" - the stale bot processed too many issues/PRs in one run and hit…
GitHub Actions hashicorp/setup-terraform wrapper conflictFix hashicorp/setup-terraform wrapper conflicts - the action wraps the terraform binary, and steps that call…