Skip to content

Fix billing:usage TDZ crash in dataforseo-account-usage - #264

Open
edenlau wants to merge 1 commit into
every-app:mainfrom
edenlau:fix/billing-usage-tdz
Open

Fix billing:usage TDZ crash in dataforseo-account-usage#264
edenlau wants to merge 1 commit into
every-app:mainfrom
edenlau:fix/billing-usage-tdz

Conversation

@edenlau

@edenlau edenlau commented Sep 1, 2026

Copy link
Copy Markdown

Problem

pnpm billing:usage authenticates and prints the account balance, then crashes:

ReferenceError: Cannot access 'FUNCTION_TOTALS' before initialization
    at printFunctionTable (scripts/dataforseo-account-usage.ts:84)

Cause

The script invokes await main() at module top level (line 10) above the module-level const FUNCTION_TOTALS declaration. Top-level await suspends module evaluation at that statement, so when main() resumes after the fetchUserData() call, the rest of the module body — including the FUNCTION_TOTALS initializer — has still never run, and printFunctionTable hits the temporal dead zone.

Fix

Move the await main() invocation to the end of the file, after all module-level declarations — the same pattern seed-rank-tracking.ts and d1-default-project-cleanup.ts already use. Pure statement move, no behavior change otherwise.

Verification

  • pnpm billing:usage against the real (free) GET /v3/appendix/user_data endpoint now prints the balance and both rolling-window spend tables with no crash.
  • tsc --noEmit and oxlint --type-aware pass.
  • Checked the other top-invoking scripts (backlinks-cost-profile.ts, brand-lookup-cost-profile.ts): they declare all module consts before their await main(), so they are unaffected.

🤖 Generated with Claude Code

`await main()` ran at module top level before the `FUNCTION_TOTALS`
const declared below it. Top-level await suspends module evaluation, so
main() resumed after the API call while the rest of the module body had
still not run, and printFunctionTable crashed with "ReferenceError:
Cannot access 'FUNCTION_TOTALS' before initialization". Move the
invocation to the end of the file, after all module-level declarations,
matching seed-rank-tracking.ts and d1-default-project-cleanup.ts.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant