Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
How can I solve 'ENOENT' error in next.js api section?

I am woking on a Next.js Project and in there I need to read data using an api and for that I have created 1 api route in /pages/api directory and that Api is using a function from `/store/manipulate.js' and I have imported that particular function in my api Route file but when I request on that api route it is giving me an error.

So, the component below is requesting for data on that api route.

Homepage.jsx

import axios from 'axios'
import { useEffect, useState } from 'react'
import EventList from '../components/events/EventList'

const HomePage = () => {

  const [featuredEvents, setFeaturedEvents] = useState([])

  useEffect(() => {
    (async () => {
      const res = await axios.get('/api/getfeaturedevents')

      if (res.status(200)) {
        console.log(res.data)
        setFeaturedEvents(res.data)
        return
      }

      console.log(res.status)
    })()
  }, [])

  return (
    <div>
      <h1>
        The Home Page.
      </h1>
      <EventList items={featuredEvents} />
    </div>
  )
}

export default HomePage

Now, the code shown below is the code of api route file

getFeaturedEvents.js

import { fetchFeaturedEvents } from '../../store/manipulate'

const getFeaturedEvents = (req, res) => {
    if (req.method === 'GET') {
        try {
            const data = fetchFeaturedEvents()

            res.send(data)
        } catch (error) {
            console.log(error)
            res.status(500).send()
        }
    }
}

export default getFeaturedEvents

Now, let's take a look at manipulate.js which will just update store.json present in same folder.

manipulate.js

const path = require('path')
const fs = require('fs')

const readData = () => {
    const data = JSON.parse(fs.readFileSync(path.join(__dirname, 'store.json')))
    return data
}

export const fetchFeaturedEvents = () => {
    let data = readData()
    const result = data.filter(event => event.isFeatured)

    return result
}

Now, the Error I am getting is...

Error Image

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Like you said @juliomalves I just changed my __dirname to process.cwd() in manipulate.js file in readData() method.

Something like this

const readData = () => {
    const data = JSON.parse(fs.readFileSync(path.join(process.cwd(), 'store', 'store.json')))
    return data
}
about 4 years ago · Juan Pablo Isaza Relatório

0

In my case, I removed the default API folder in pages

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda