node.js - NextJS `cookies` was called outside a request scope when turning off JavaScript in browser - Stack Overflow

admin2025-04-16  3

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.

Share Improve this question asked Feb 3 at 12:53 Dev-LaJuDev-LaJu 114 bronze badges 2
  • Is this a real problem? Things aren't supposed to work as expected when JS is disabled – Estus Flask Commented Feb 3 at 12:53
  • @EstusFlask I want to support browsers without JS-support. Since it is a "normal" form POST-request without JS, I expected the client to send cookies. – Dev-LaJu Commented Feb 3 at 16:48
Add a comment  | 

1 Answer 1

Reset to default 0

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.

转载请注明原文地址:http://www.anycun.com/QandA/1744770755a87387.html