Using Next.js 15.1.6. I keep getting Error: `cookies` was called outside a request scope
, when trying to access cookies in a server action.
The server action is called from a client component using action={...}
on a form.
File testAction.ts
"use server";
import { cookies } from "next/headers";
export async function checkCookiesAction() {
const cookieStore = await cookies();
console.log(cookieStore.getAll());
}
File page.tsx
"use client";
import { checkCookiesAction } from "./testAction";
export default function Page() {
return (
<form action={checkCookiesAction}>
<button type="submit">Login</button>
</form>
);
}
It works when JS is enabled in browser, but throws error if it is disabled; then I get the following error:
⨯ Error: `cookies` was called outside a request scope. Read more:
at cookies (app/login/testAction.ts:5:35)
I've tried running it in incognito mode (with JavaScript off), but still the same error.
Using Next.js 15.1.6. I keep getting Error: `cookies` was called outside a request scope
, when trying to access cookies in a server action.
The server action is called from a client component using action={...}
on a form.
File testAction.ts
"use server";
import { cookies } from "next/headers";
export async function checkCookiesAction() {
const cookieStore = await cookies();
console.log(cookieStore.getAll());
}
File page.tsx
"use client";
import { checkCookiesAction } from "./testAction";
export default function Page() {
return (
<form action={checkCookiesAction}>
<button type="submit">Login</button>
</form>
);
}
It works when JS is enabled in browser, but throws error if it is disabled; then I get the following error:
⨯ Error: `cookies` was called outside a request scope. Read more: https://nextjs.org/docs/messages/next-dynamic-api-wrong-context
at cookies (app/login/testAction.ts:5:35)
I've tried running it in incognito mode (with JavaScript off), but still the same error.
I've been hitting this error as well, so did some testing.
It looks like this is being caused by a bug that has already been fixed in Next.js - it seems to have been introduced in 15.1.0, and it is fixed in the 15.2.0 canary releases.
Downgrading Next.js to 15.0.4 fixed the issue for me.