next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Next App Router APIs unexpected outputs and Console.log not working

Open vlohar08 opened this issue 2 years ago • 6 comments

Link to the code that reproduces this issue or a replay of the bug

https://github.com/vlohar08/Nextjs-API-Issue

To Reproduce

  1. Clone the mentioned Github Repo.
  2. Run yarn and yarn build
  3. Start the Nextjs project using yarn start
  4. Hit this http://localhost:3000/api/auth/google/spreadsheets API using any tool.

Current vs. Expected behavior

Current Behaviour It returns "NOT OK" image

Expected Behaviour Return and Console log the Request.url value

Verify canary release

  • [X] I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 10 Pro
    Binaries:
      Node: 18.16.0
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.20-canary.33
      eslint-config-next: 13.4.19
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.2.2
    Next.js Config:
      output: N/A

Which area(s) are affected? (Select all that apply)

App Router

Additional context

app/api/auth/google/spreadsheets/route.ts

import { NextRequest, NextResponse } from "next/server";

export async function GET(request: NextRequest) {
  try {
    console.log(request.url);
    return NextResponse.json(request.url);
  } catch (error) {
    return NextResponse.json("NOT OK");
  }
}

No response

vlohar08 avatar Sep 16 '23 14:09 vlohar08

It works using latest LTS release of Node. Update Node from 18.2.0 to latest LTS, and try again.

kddige avatar Sep 16 '23 14:09 kddige

@kddige Tried Node v18.7.1 and Node v20.6.1 but it is still not working and getting the same response.

vlohar08 avatar Sep 16 '23 16:09 vlohar08

Am running into a somewhat similar issue where some routes under app/api are swallowing up console.log statements and function returns. Bizarrely, in one of my apps it is only 3 out of six routes.

peterdresslar avatar Oct 01 '23 03:10 peterdresslar

Hi,

I have a very similar issue:

  • If I run npm run dev the GET endpoint works
  • If I run npm run build && npm run start the GET endpoint does NOT work
  • If I change the endpoint from GET to POST, it always works!

Example code -- the print is done only with run dev:

export async function GET(_req: NextRequest): Promise<NextResponse>
{
    console.log("GET called")
    return NextResponse.json({})
}
  • Node version: v21.1.0
  • Next version: 14.1.0

Regards

FStefanni avatar Feb 12 '24 11:02 FStefanni

Reproducible on my end as well, console logs are being swallowed by the app router api route. Node version: 20.10.0 Next version: 14.2.3

Edit: My api route was "static" because that is the default behavior on the app router side, and as soon as I made it dynamic, the logs started showing up normally like they should. So apparently logs won't show up only if your api route is static.

vipul0092 avatar Jun 06 '24 11:06 vipul0092

This issue has been automatically marked as stale due to inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

nextjs-bot avatar Dec 06 '25 23:12 nextjs-bot

This issue has been automatically closed due to inactivity. If you’re still experiencing a similar problem or have additional details to share, please open a new issue following our current issue template. Your updated report helps us investigate and address concerns more efficiently. Thank you for your understanding!

nextjs-bot avatar Dec 14 '25 23:12 nextjs-bot