Skip to content
Latchkey

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

  1. Replace import with require inside the script.
  2. Use the injected github, context, and core globals directly.
  3. 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.

Frequently asked questions

What causes ""ReferenceError: require is not defined""?
import x from y is not valid in the CommonJS context the action evaluates; use require or the provided globals.
How do I fix "ReferenceError: require is not defined"?
Use CommonJS and provided globals

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card