gatsbyjs/gatsby · error

Error when executing function "${functionObj.originalAbsolut

Error message

Error when executing function "${functionObj.originalAbsoluteFilePath}":<br /><br />${e.message}

What it means

createSetContextFunctionMiddleware requires each compiled Gatsby function module; failures here mean the file could not be loaded (syntax error, missing dependency, or no default function export — that case gets a tailored message). The error is reported and an HTML error response is prepared for the request.

Source

Thrown at packages/gatsby/src/internal-plugins/functions/middleware.ts:141

      const pathToFunction = functionObj.absoluteCompiledFilePath
      let fnToExecute
      try {
        delete require.cache[require.resolve(pathToFunction)]
        const fn = require(pathToFunction)
        userConfig = fn?.config

        fnToExecute = (fn && fn.default) || fn
      } catch (e) {
        if (e?.message?.includes(`fnToExecute is not a function`)) {
          e.message = `${functionObj.originalAbsoluteFilePath} does not export a function.`
        }

        fnToExecute = undefined
        reporter.error(e)
        if (!res.headersSent) {
          if (showDebugMessageInResponse) {
            res
              .status(500)
              .send(
                `Error when executing function "${functionObj.originalAbsoluteFilePath}":<br /><br />${e.message}`
              )
          } else {
            res.sendStatus(500)
          }
        }
        return
      }

      if (fnToExecute) {
        const { config, warnings } = createConfig(userConfig)

        printConfigWarnings(warnings, functionObj)

        req.context = {
          functionObj,
          fnToExecute,

View on GitHub (pinned to e85d62f177)

Solutions

  1. Ensure the function file exports a default function
  2. Fix syntax/import errors in the function file
  3. Check the function compiles (see reporter output) before redeploying
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at packages/gatsby/src/internal-plugins/functions/middleware.ts:141 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of gatsbyjs/gatsby@e85d62f177 (2026-08-26). Data as JSON: /api/errors/1fe17ad2d2e32f6e. Report an issue: GitHub.