Skip to content

Diagnostic reference

ReferenceError: X is not defined

Why Apps Script reports a name as not defined: typo, scope gap, unsaved file, or cross-file load order, and how to tell them apart.

Updated September 12, 2026

Apps Script error

ReferenceError: formatDate is not defined

What it means

The name does not exist in scope when the line runs. The causes split four ways: a typo, a name declared inside another function, a function in a file that failed to save, or a top-level constant in another .gs file evaluated after the code that reads it. Apps Script shares one global scope across files, but evaluation order still matters for eager top-level code.

Related error messages

  • ReferenceError: Utils is not defined
  • ReferenceError: Cannot access 'CONFIG' before initialization

Before and after

See the change before you make it.

Problem
function report() {
  const label = formatDate(new Date());
  Logger.log(label);
}

function formatDate_(d) {
  return Utilities.formatDate(d, Session.getScriptTimeZone(), 'yyyy-MM-dd');
}
Safer version
function report() {
  const label = formatDate_(new Date());
  Logger.log(label);
}

function formatDate_(d) {
  return Utilities.formatDate(d, Session.getScriptTimeZone(), 'yyyy-MM-dd');
}

How to fix it

  • Compare the call with the declared function name, including the trailing underscore.
  • Call formatDate_ so the name matches the declaration.
  • For other cases, check scope and move eager initialization into a function.

What stays with you

  • For cross-file load-order failures, move eager work into functions; renaming alone is not enough.
  • myFunction does not change Apps Script file evaluation order.