Next.js Discord

Discord Forum

Cannot Import Function from another file

Answered
shadow posted this in #help-forum
Open in Discord
Hey devs, currently im trying import an function that is implemented at:

.
└── utils/
    └── Auctions.tsx


and the file where im trying to use the function is at:
.
└── app/
    └── api/
        └── auction/
            └── [auctionID]/
                └── route.js

this is the function im trying to import:

export async function fetchDetails(auctionID: string) {
  const response = await fetch(
    `https://api.example.com/api/auctions/${auctionID}`
  );
  const data = await response.json();
  return data;
}


but when i try to call that function on route.js i get nothing.

route.js
import { NextResponse } from "next/server";
import { fetchDetails } from "@utils/Auctions";

export async function GET(req, { params }) {
  const auctionID = params.auctionID;
  if (!auctionID) {
    return NextResponse.json({ error: "No Auction ID provided." }, { status: 400 });
  }

  const data = await fetchDetails(auctionID);
  return NextResponse.json(data, { status: 200 });
}


tsconfig.json
{
  "compilerOptions": {
    /...
    "paths": {
      "@/*": ["./*"],
      "@utils/*": ["./utils/*"],
    }
  },
  /...
}
Answered by European sprat
but you don't actually need @utils since @/* will allow you to use @/utils/
View full answer

6 Replies

European sprat
Maybe you need to define utils before @/* ? in tsconfig
European sprat
but you don't actually need @utils since @/* will allow you to use @/utils/
Answer
@European sprat but you don't actually need @utils since @/* will allow you to use @/utils/
so i should import the function like this?

import { fetchDetails } from "@/utils/Auctions";


tsconfig.json
{
  "compilerOptions": {
    /...
    "paths": {
      "@/*": ["./*"],
    }
  },
  /...
}
European sprat
does it work?
@European sprat does it work?
i think the problem is from the api itself, im getting SyntaxError: Unexpected end of JSON input
oh my god i had an syntax error in the endpoint lmao