Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

159
Views
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 answers
Answer question

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 Report

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!