Cannot use "googleapis" package in SvelteKit route on Pages

In SvelteKitwith cloudflare pages, the recommended way to add functions is to use the normal sveltekit +server.ts route. This seems to work fine when i don't import any node modules, but I want to update a google spreadsheet from within a function. The issue is that when I import any "googleapis" the handler doesn't even run. I have the node_js compat flag set The handler gets hit (according to the wrangler logs) but immediately responds with this 500 error without running the function.
POST https://mysite.pages.dev/api/addEmailToSheet - Ok @ 5/9/2025, 12:49:28 AM
(error) TypeError: Cannot convert object to primitive value
POST https://mysite.pages.dev/api/addEmailToSheet - Ok @ 5/9/2025, 12:49:28 AM
(error) TypeError: Cannot convert object to primitive value
The SvelteKit /api/addEmailToSheet/+server.ts file
import { json, type RequestHandler } from "@sveltejs/kit";
import { sheets } from "googleapis/build/src/apis/sheets";
import { auth } from "googleapis/build/src/apis/sheets";

const SCOPES = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file',
];

export const POST = (async ({ request }) => {
// Log #0: The very first thing. If this doesn't show, the problem is almost certainly external.
console.log("[MINIMAL_HANDLER LOG 0] Handler called.");

try {
// Cautiously check and log the event properties
console.log("[MINIMAL_HANDLER LOG 1] Request method:", request.method);
console.log("[MINIMAL_HANDLER LOG 2] Request URL:", request.url);
console.log("[MINIMAL_HANDLER LOG 3] Request headers:", request.headers);


return json({ success: true, message: "Minimal handler executed." });

} catch (e: any) {
console.error("[MINIMAL_HANDLER CATCH] Error within minimal handler execution:", e.message);
if (e.stack) {
console.error("[MINIMAL_HANDLER STACK]", e.stack);
}
return json({ success: false, error: "Error within minimal handler.", details: e.message }, { status: 500 });
}
}) satisfies RequestHandler;
import { json, type RequestHandler } from "@sveltejs/kit";
import { sheets } from "googleapis/build/src/apis/sheets";
import { auth } from "googleapis/build/src/apis/sheets";

const SCOPES = [
'https://www.googleapis.com/auth/spreadsheets',
'https://www.googleapis.com/auth/drive.file',
];

export const POST = (async ({ request }) => {
// Log #0: The very first thing. If this doesn't show, the problem is almost certainly external.
console.log("[MINIMAL_HANDLER LOG 0] Handler called.");

try {
// Cautiously check and log the event properties
console.log("[MINIMAL_HANDLER LOG 1] Request method:", request.method);
console.log("[MINIMAL_HANDLER LOG 2] Request URL:", request.url);
console.log("[MINIMAL_HANDLER LOG 3] Request headers:", request.headers);


return json({ success: true, message: "Minimal handler executed." });

} catch (e: any) {
console.error("[MINIMAL_HANDLER CATCH] Error within minimal handler execution:", e.message);
if (e.stack) {
console.error("[MINIMAL_HANDLER STACK]", e.stack);
}
return json({ success: false, error: "Error within minimal handler.", details: e.message }, { status: 500 });
}
}) satisfies RequestHandler;
Note that just importing the handler causes it to fail.
1 Reply
Xenoderf
XenoderfOP7mo ago
I could get other functions working like a basic GET and POST handler if they did not import certain libraries. So i think that importing certain libraries causes some weird build output that causes the endpoints to fail

Did you find this page helpful?