Next.js Discord

Discord Forum

API filelocation

Answered
Asian black bear posted this in #help-forum
Open in Discord
Asian black bearOP
Hi everyone im using Next.js 14.1.4 with the App library.
im currently trying to build my application but everytime i do this it fails.
So to test why its fails i tried to remove my API folder and sudenly it works.

I was wondering if the filepath was correct.

So i currently have app/api/users/route.tsx

Is this the correct filepath and way to correctly program api-routes
i also used
app/api/users/[id]/route.tsx for example

the api's are programmed with the:
export async function GET (req: NextApiRequest){

And not with any function named handler

thanks in advance !
Answered by Asian black bear
Downgrading to 13.5.6 worked for me
View full answer

46 Replies

may you explain more, what do you mean by it fails?
@averydelusionalperson may you explain more, what do you mean by it *fails*?
Asian black bearOP
â–² Next.js 14.1.4

Creating an optimized production build ...
Compiler server unexpectedly exited with code: 3221225725 and signal: null
> Build error occurred
Error: Jest worker encountered 1 child process exceptions, exceeding retry limit
    at ChildProcessWorker.initialize (F:\Armani\New folder\armani\node_modules\next\dist\compiled\jest-worker\index.js:1:11580)
    at ChildProcessWorker._onExit (F:\Armani\New folder\armani\node_modules\next\dist\compiled\jest-worker\index.js:1:12545)
    at ChildProcess.emit (node:events:530:35)
    at ChildProcess._handle.onexit (node:internal/child_process:294:12) {
  type: 'WorkerError'
}
you get this while running next build ?
Asian black bearOP
yes
try removing .next, node_modules (optional) & build again
@averydelusionalperson try removing `.next`, `node_modules (optional)` & build again
Asian black bearOP
you mean the .next folder or the whole package
just the .next folder
Asian black bearOP
same error already tried it
this happens on multiple devices
also tried to build it on my server and it gives the same error when i installed the node_modules again
hmm, you're using jest?
Asian black bearOP
nope
what's your node version?
Asian black bearOP
20.12.0
you say, you don't get the error when you remove the api routes?
Asian black bearOP
yes when i remove the whole api directory outside the app directory it builds perfect
ahhhh, we can use .tsx extension in api routes?
@averydelusionalperson ahhhh, we can use `.tsx` extension in api routes?
Asian black bearOP
can or cant?
I'm asking you lmao, I'm not sure, never tried it
Asian black bearOP
i dont know either
first time im programming in next.js
should i change the extension
try it ig?
Asian black bearOP
so change it to .ts?
yeah
Asian black bearOP
nope same error
ugh
not sure what the error is, only getting it when added the api routes
is it any particular route or api folder in whole?
@averydelusionalperson is it any particular route or api folder in whole?
Asian black bearOP
gonna try to filter it now
yeah, try it
Asian black bearOP
nope its doesnt matter which folder/route you use
it still crashes


maybe i forgot something in the route

so here is an example of one of my scripts:
right now only this app/api/users/route.ts exists in the project
i've temporary deleted the others
import { NextApiRequest } from "next";
import {  promisePool } from "@/lib/db";
import { NextResponse } from "next/server";




export async function GET (req: NextApiRequest){

    try {

        const [rows] = await promisePool.query("SELECT * FROM users");
        // opdrachten contains rows returned by server
        return NextResponse.json(rows);



    } catch (error) {
        console.error('Error fetching challenges:', error);
        return NextResponse.json({error: 'Error fetching challenges:'},{status: 500});


    }
}
can you remove the NextApiRequest import & just use Request?
Asian black bearOP
import {  promisePool } from "@/lib/db";
import { NextResponse } from "next/server";

export async function GET (req: Request){

    try {

        const [rows] = await promisePool.query("SELECT * FROM users");
        // opdrachten contains rows returned by server
        return NextResponse.json(rows);

    } catch (error) {
        console.error('Error fetching challenges:', error);
        return NextResponse.json({error: 'Error fetching challenges:'},{status: 500});

    }
}


like this doesnt work
same error when building
works good in local?
I mean in dev
Asian black bearOP
yeo
yep
fully works on dev
Asian black bearOP
my ts version is 4.8.2 is this the error maybe?
not sure, try to update maybe?
Asian black bearOP
found the problem i think:

its import { promisePool } from "@/lib/db";
do we have any alternative for this?
because i think copilot or a yt video just gave me this option
Asian black bearOP
nope the error looks like its because of this part:
        const [rows] = await promisePool.query("SELECT * FROM users");


my db.ts look s like this:

import * as mysql2 from 'mysql2';

export const pool = mysql2.createPool({
    host: process.env.DB_HOST,
    user: process.env.DB_USER,
    password: process.env.DB_PASS,
    database: process.env.DB_NAME,
    connectionLimit: 10,
    idleTimeout: 60000,
});

export const promisePool = pool.promise();
Asian black bearOP
Downgrading to 13.5.6 worked for me
Answer
@Asian black bear Downgrading to 13.5.6 worked for me
Arboreal ant
got the same problem, you fixed it by downgrading your next vesion?
@Arboreal ant got the same problem, you fixed it by downgrading your next vesion?
Asian black bearOP
yeh it was downgrading to 13.5.6

But eventually after a few days that also didnt work
and updating to next@canary worked for me again
idk what it was or is