Getting a variable from a JSON file and then modifying the file in a Next.js project deployed to Vercel

Viewed 27

I'm using a JSON file to mock a database for some testing. I'm calling and modifying this JSON from Next.js API routes.

This is working locally but when I deploy it to Vercel I'm getting a 500 error.

const fs = require('fs');

let users = require('data/users.json');

export const usersDBmock = {
  delete: _delete
};


function _delete(id) {
  users = users.filter((x) => x.id !== id);
  saveData();
}


function saveData() {
   fs.writeFileSync('data/users.json', JSON.stringify(users));
}

In my /pages/api endpoint I'm calling

usersDBmock.delete(123);

Error I'm getting

    "error": {
        "errno": -30,
        "syscall": "open",
        "code": "EROFS",
        "path": "data/users.json"
    }

My suspicious is that 'data/users.json'is not the correct path in Vercel but I can figure out how to do it.

Any ideas?

0 Answers
Related