4

I'm trying to set cookies inside the server actions function in Next.js 13.4.1. Example code

import { cookies } from 'next/headers';
 
async function create(data) {
  'use server';
  cookies().set('name', 'lee');
  // or
  cookies().set('name', 'lee', { secure: true });
  // or
  cookies().set({
    name: 'name',
    value: 'lee',
    httpOnly: true,
    path: '/',
  });
}

But i got error with message: Property 'set' does not exist on type 'ReadonlyRequestCookies'

Am I missing something?

yamada kun
  • 61
  • 1
  • 4

1 Answers1

0

That's strange, because it should normally work.

maybe you using it in a "use client" component? you can"t use it there. Use "cookies-next" instead.

in my project server side components i useconst nextjsCookies = cookies(); to initialise the cookies and set the cookie with nextjsCookies.set("test", "test");

Samir
  • 21
  • 2