Skip to content
Latchkey

Salesforce "Cannot read property accessToken" in CI

The CLI loaded an org connection whose auth fields are undefined, then dereferenced accessToken on it. The org was never authorized in this environment, or its cached auth file is empty or corrupt.

What this error means

A command crashes with "Cannot read property 'accessToken' of undefined" or "Cannot read properties of undefined (reading 'accessToken')" instead of a clean auth error.

sf
TypeError: Cannot read properties of undefined (reading 'accessToken')

Common causes

No valid auth record for the target org

The alias resolves to an org with no usable token, so the connection object lacks accessToken. This follows a failed or skipped login.

A stale or corrupted auth cache

A partially written auth file (interrupted login, restored cache) leaves the org record without credentials.

How to fix it

Re-authenticate cleanly

  1. Log out the alias to clear any partial record.
  2. Run the JWT or auth-URL login again in this job.
  3. Confirm sf org display --target-org <alias> returns an access token.
Terminal
sf org logout --target-org ci-org --no-prompt || true
sf org login jwt --alias ci-org ...
sf org display --target-org ci-org

Do not cache the CLI auth directory blindly

If you cache ~/.sfdx/~/.sf across runs, a stale entry can yield an empty token. Prefer re-authenticating each run over restoring auth caches.

How to prevent it

  • Authenticate fresh in each job rather than caching auth state.
  • Verify the org with sf org display before commands that need a token.
  • Keep the CLI updated so auth-record handling stays current.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →