NextJs 14.2.2 middleware "You're importing a component that needs next/headers.... etc" #64824
Labels
bug
Issue was opened via the bug report template.
invalid link
The issue was auto-closed due to a missing/invalid reproduction link. A new issue should be opened.
Link to the code that reproduces this issue
not
To Reproduce
Use NextJs 14.2.2
Document: https://nextjs.org/docs/app/building-your-application/authentication
For example:
middleware.ts
`
...
import { NextRequest, NextResponse } from 'next/server'
import { decrypt } from '@/app/lib/session'
import { cookies } from 'next/headers'
// 1. Specify protected and public routes
const protectedRoutes = ['/dashboard']
const publicRoutes = ['/login', '/signup', '/']
...
`
Get error:
Error:
× You're importing a component that needs next/headers. That only works in a Server Component which is not supported in the pages/ directory. Read more: https://nextjs.org/docs/getting-started/
│ react-essentials#server-components
│
│
╭─[/hide/src/middleware.ts:1:1]
1 │ import {localePrefix, locales, pathnames} from "@/src/config";
2 │ import {NextRequest, NextResponse} from "next/server";
3 │ import createIntlMiddleware from 'next-intl/middleware';
4 │ import { cookies } from 'next/headers'
· ──────────────────────────────────────
5 │
╰────
What is the problem?
Current vs. Expected behavior
Success execute
Provide environment information
Operating System: Platform: darwin Arch: x64 Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:10:42 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6000 Available memory (MB): 32768 Available CPU cores: 10 Binaries: Node: 20.11.1 npm: 10.2.4 Yarn: N/A pnpm: N/A Relevant Packages: next: 14.2.2 // Latest available version is detected (14.2.2). eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 5.4.5 Next.js Config: output: N/A
Which area(s) are affected? (Select all that apply)
Not sure
Which stage(s) are affected? (Select all that apply)
next dev (local)
Additional context
NextJs 14.2.2
The text was updated successfully, but these errors were encountered: